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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
VulpesRank #55 · 29.2K reputationPosted Jun 29, 2012, 2:07 AM EDT
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.
Santhosh Kumar JayaramanRank #563 · 4.3K reputationPosted Jun 29, 2012, 2:15 AM EDT
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;
}
}
VulpesRank #55 · 29.2K reputationPosted Jun 29, 2012, 2:30 AM EDT
Although it's seldom done, you can use a different accessor for the 'get' as well.
The following compiles and runs fine:
Santhosh Kumar JayaramanRank #563 · 4.3K reputationPosted Jun 29, 2012, 2:35 AM EDT