Ahhhh ... Different Levels of Accessibility on Property Accessors

Today I had the opportunity to take advantage of a .NET 2.0 little gem that allows you to set different accessibility levels on your property getters/setters.  I got so used to doing work arounds in 1.1 that I almost didn't use it.  Ahhh ... the little things :-)

public int MyValue
{
   get return _myValue; }

   protected set { _myValue = value;}
}

PS. This can also be done for indexers.

More on MSDN.

 

Comments

# Erik Porter said:

Yes! That's one of my favorites! ;)

Saturday, April 30, 2005 1:20 AM
# Craig Boland said:

VB6 had that capability. It was quite useful. Since then, if I wanted to set a read-only property from outside the class, such as when loading from data source, I would implement something like:

internal SetMyReadOnlyProperty(string value){
_myReadOnlyProperty = value;
}

private string _myReadOnlyProperty = string.Empty;
public string MyReadOnlyProperty{
get{ return _myReadOnlyProperty; }
}

Thanks for the tip, K!

Monday, May 02, 2005 6:07 PM

Leave a Comment

(required) 
(required) 
(optional)
(required)