Home > Articles > Programming > C#

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

Access Modifiers

When declaring a field earlier in the chapter, you prefixed the field declaration with the keyword public. public is an access modifier that identifies the level of encapsulation associated with the member it decorates. Five access modifiers are available: public, private, protected, internal, and protected internal. This section considers the first two.

The purpose of an access modifier is to provide encapsulation. By using public, you explicitly indicate that it is acceptable that the modified fields are accessible from outside the Employee class—in other words, that they are accessible from the Program class, for example.

Consider an Employee class that includes a Password field, however. It should be possible to call an Employee object and verify the password using a Logon() method. Conversely, it should not be possible to access the Password field on an Employee object from outside the class.

To define a Password field as hidden and inaccessible from outside the containing class, you use the keyword private for the access modifier, in place of public (see Listing 5.15). As a result, the Password field is not accessible from inside the Program class, for example.

LISTING 5.15: Using the private Access Modifier

class Employee
{
  public string FirstName;
  public string LastName;
  public string Salary;
  private string Password;                                                        
  private bool IsAuthenticated;                                                   
 
  public bool Logon(string password)                                              
  {                                                                               
      if(Password == password)                                                    
      {                                                                           
          IsAuthenticated = true;                                                 
      }                                                                           
      return IsAuthenticated;                                                     
  }                                                                               
                                                                                  
  public bool GetIsAuthenticated()                                                
  {                                                                               
      return IsAuthenticated;                                                     
  }                                                                               
  // ...
}
__________________________________________________________________________________
class Program
{
  static void Main()
  {
      Employee employee = new Employee();
 
      employee.FirstName = "Inigo";
      employee.LastName = "Montoya";
 
      // ...
 
      // Password is private, so it cannot be                                    
      // accessed from outside the class.                                        
      // Console.WriteLine(                                                      
      //    ("Password = {0}", employee.Password);                               
  }
  // ...
}

Although this option is not shown in Listing 5.15, it is possible to decorate a method with an access modifier of private as well.

If no access modifier is placed on a class member, the declaration defaults to private. In other words, members are private by default and programmers need to specify explicitly that a member is to be public.

  • + Share This
  • 🔖 Save To Your Account