Volunteer .NET Evangelist

A well oiled machine can’t run efficiently, if you grease it with water.
  首页  :: 联系 :: 订阅 订阅  :: 管理

Get & Set Accessors Can Be Of Different Accessibility

Posted on 2006-01-19 23:20  Sheva  阅读(292)  评论(0编辑  收藏  举报
    In C#2.0, get and set accessors of a property can be of different accesibility.This feature is quite useful, imagine that if you want to protect your property from being written by outside code, and you still want this property can be read by ourtside code, you can define the get accessor as public, and set accessor as protected or internal or protected internal or even private, just as the following code says:
public class MyClass
{
    
private Int32 myField;
    
public Int32 MyProperty
    {
        
get {return myField;}
        
protected set {myField = value;}
    }
}