Search 
DailyCoding > General

Specifying Access Modifier for Getter and Setter in Property

Explains about how we can have different access modifier for getter and setter in a property
Author admin on Jun 13, 2008 1 Comments
Rate it    (Rated 5 by 1 people)
202 Views

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.

C# | Utility

Discussion

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

Name
Email Address
Web Site
© Copyright 2008 Daily Coding • All rights reserved