Hi
Can we specify the access modifiers for the get and set accessors in a property ?
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Santhosh Kumar JayaramanPosted Jun 29, 2012, 6:35 AM
VulpesPosted Jun 29, 2012, 6:30 AM
Although it's seldom done, you can use a different accessor for the 'get' as well.
The following compiles and runs fine:
Santhosh Kumar JayaramanPosted Jun 29, 2012, 6:15 AM
private DataTable _countries;
public DataTable Countries
{
get
{
return _countries;
}
private set
{
_countries = value;
}
}
also
public DataTable Countries
{
get
{
return _countries;
}
protected set
{
_countries = value;
}
}
and also
public DataTable Countries
{
get
{
return _countries;
}
internal set
{
_countries = value;
}
}
VulpesPosted Jun 29, 2012, 6:07 AM
However, you can use a different access modifier on either the get or set accessor provided that this does not make it more accessible than the property itself.
For example, in a public property, you could make the set accessor internal or private.