Accessors
The get and set portions of a property or indexer are called accessors. By default these accessors have the same visibility, or access level. However, it is sometimes useful to restrict access to one of these accessors. Typically, this involves restricting the accessibility of the set accessor, while keeping the get accessor publicly accessible. For example:
public string Name
{
get
{
return name;
}
protected set
{
name = value;
}
}
In this example, a property called Name defines a get and set accessor. The get accessor receives the accessibility level of the property itself, public in this case, while the set accessor is explicitly restricted by applying the protected access modifier to the accessor itself.
public string Name
{
get
{
return name;
}
protected set
{
name = value;
}
}
In this example, a property called Name defines a get and set accessor. The get accessor receives the accessibility level of the property itself, public in this case, while the set accessor is explicitly restricted by applying the protected access modifier to the accessor itself.
C# provides Restrictions on Access Modifiers on Accessors. The access modifiers for the property accessor (get or set) must be more restrictive than the property access modifier. For example, if the property access modifier is internal, then get or set accessor access modifier must be less restrictive like private etc. It cannot be public because public is not more restrictive than internal.
ReplyDelete.Net training in Chennai