C# Tutorial - Access Modifiers and Properties

posted Jun 28, 2011, 7:15 PM by magic stone

Each part of an application, whether it is a class, structure, enumeration or other object can be set so that another part of the application can either see it or not. This is what makes encapsulation and data-hiding work, as we have previously seen and is achieved through the use of access modifiers. The most commonly used modifiers are privateprotected and public.

An attribute or method marked private can only be seen from within the class that it is defined within. When using data-hiding, all attributes are marked private. If you mark something private outside of a class or struct, a compile time error will occur.

An attribute or method marked public can be accessed from any class that has access to the class members. There is no restriction on accessing public members. When using data-hiding, the methods used to access the private attributes will be declared public.

An attribute or method marked protected can only be seen from within the class that it is defined within or from within a class that inherits from the class that it is defined within. This is useful when allowing derived classes access to the attributes of the base class.

C# defines a fourth access modifier, internal, which allows members to be accessible from within the same assembly of which it is defined. This is a good method of allowing several classes to share some common data.

Finally a fifth access modifier, protected internal, this can be seen as protected OR internal, and not protected ANDinternal for which there is no concept.

In general, attributes of a class are designated as private and the methods that act upon the attributes are designated aspublic.

  • If you do not give a class an access modifier, it will default to public, because a class cannot be private unless it is an inner class.

  • If you do not give a method an access modifier, it is defaulted as private.

  • If you do not give an attribute an access modifier, it is defaulted as private.

Access ModifierRestrictions
publicNo restrictions. Attributes and methods marked public are visible to any method of any class.
privateThe Attributes and methods in class A which are marked private are accessible only to methods of class A.
protectedThe Attributes and methods in class A which are marked protected are accessible to methods of class A and also to methods of classes derived from class A.
internalThe Attributes and methods in class A which are marked internal are accessible to methods of any class in A's assembly.
protected internalThe members in class A which are marked protected internal are accessible to methods of class A, to methods of classes derived from class A, and also to any class in A's assembly.

Properties

Properties allow users to access class variables as if they were accessing member fields directly, while actually implementing that access through a class method.

The user wants direct access to the variables of the object and does not want to work with methods. The class designer, however, wants to hide the internal variables of his class in class members, and provide indirect access through a method. By decoupling the class variables from the methods that access those variables, the designer is free to change the internal state of the object as needed.

Coding Properties

The code below shows how to create a private variable with its associated properties.

// private member variables
private int hour;

// create a property
public int Hour
{
  get { return hour; }
  set { hour = value; }
}

We would access the properties in the following manner:

// Get the current value of hour to local variable iHour
int iHour = aClass.Hour;

// Increment iHour
iHour++;

// Write iHour back to hour
aClass.Hour = iHour;

Comments