interviews.dotnetthread.com

↑ Grab this Headline Animator

Tuesday, January 13, 2009

Can we have different access modifier for get or set accessor with that of property in a property.

Yes, Only thing which we need to take care is that accessibility modifier of the get or set accessor must be more restrictive than the property. Also we Cannot specify accessibility modifiers for both accessors of the property at a time.



Ex:

private string _Name;

public string Name
{
protected get { return _Name; }
set { _Name = value; }
}

In the above example we have protected get and internal set accessors in a public property.
We cant have accessibility modifiers for both accessors of the property at a time, like we cant have internal for set accessor.

The same example can be written as below which is called Automatic Property in C# 3.0

Ex:
public string Name
{
protected get;
set;
}

Submit this story to DotNetKicks

No comments:

Post a Comment

Post your comments/questions/feedback for this Article.

 

Latest Articles