Specifying Access Modifier for Getter and Setter in Property

We normally specify the access modifier of a property during its declaration before the return type.

// getter and setter are public
public string EmployeeCode
{
  get 
  { 
    return _employeeCode; 
  }
  set
  {
    _employeeCode = value;
  }
}

This access modifier will be applicable to both getter and setter of the property by default. However there is an interesting fact in .net, we can have different access modifier for both getter and setter. To specify that

// getter is publilc and setter is internal
public string EmployeeCode
{
  get 
  { 
    return _employeeCode; 
  }
  internal set
  {
    _employeeCode = value;
  }
}

There is one thing to keep remember is that you cannot specify access modifier for both getter and setter at same time. The other will always take the default from property. However this doesn’t matter to us as we can achieve any combination from available flexibility.

1 comment(S)


Viral on Aug 22, 2008 12:32 AM

Hello,

As i am not aware of this fact about we can specify different access Modifier for getter/setter.

Thanks for that

Leave a comment