Private

The private specifier is the most secure access specifier available in the C# programming language. By setting a class or member of a class as private, you are determining that the class or the member won't be allowed to be accessed by other classes. The scope of a private member is within the class. For example, if you create a private field, that field can't be accessed outside the class. That private field can only be used internally in that class.

Let's look at an example of a class with a private field:

public class Animal {
private string name;
public string GetName() {
return name;
}
}

Here, as the GetName() method and the private field name are in the same class, the method can access the field. However, if another method outside of the Animal class tries to access the name field, it won't be able to.

For example, in the following code, the Main method is trying to set the private field name, which is not permissible:

using System;
namespace AnimalProject {
static void Main(){
Animal animal = new Animal();
animal.name = "Dog"; // Not possible, as the name field is private
animal.GetName(); // Possible, as the GetName method is public
}
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.219.132.107