Hi, i know that we should not have access modifiers for both get and set of a property. can anyone pl. tell me the reason for it.. Thanks & Regards, Anil.
Normally, the get and set accessors of a property have the same access modifier, namely the one placed on the property as a whole: private int myInt; public int MyInt { get { return myInt;} set { myInt = value;} } Here both the getter and setter are implicitly public. However, this need not be the case. If you want, you can make one accessor (usually the setter) less accessible than the other: private int myInt; public int MyInt { get { return myInt;} internal set { myInt = value;} } Here, the setter is internal( i.e. you can only call it from within the same assembly) but the getter is still public.
Thanks a lot for your valuable reply. so, can i assume that, setter access modifier should be relavant to that of getter. what i mean is if getter property is private then the access modifier of setter should be private and not public.. Am i right? Thanks & Regards, Anil
If the whole property is public, then both the gettter and setter are public <b>unless</b> one of them is qualified with a more restrictive access such as internal, protected or private. In my experience, mixed accessibility isn't often used but it's there if you need it.
1 answers
Normally, the get and set accessors of a property have the same access modifier, namely the one placed on the property as a whole:
private int myInt;
public int MyInt
{
get { return myInt;}
set { myInt = value;}
}
Here both the getter and setter are implicitly public.
However, this need not be the case. If you want, you can make one accessor (usually the setter) less accessible than the other:
private int myInt;
public int MyInt
{
get { return myInt;}
internal set { myInt = value;}
}
Here, the setter is internal( i.e. you can only call it from within the same assembly) but the getter is still public.
answered 2 years ago by:
17279
Thanks a lot for your valuable reply. so, can i assume that, setter access modifier should be relavant to that of getter. what i mean is if getter property is private then the access modifier of setter should be private and not public.. Am i right?
Thanks & Regards,
Anil
answered 2 years ago by:
57
If the whole property is public, then both the gettter and setter are public <b>unless</b> one of them is qualified with a more restrictive access such as internal, protected or private.
In my experience, mixed accessibility isn't often used but it's there if you need it.
answered 2 years ago by:
17279
Thank you, thanks a lot for your valuable reply,
Have a great time,
Anil
answered 2 years ago by:
57
This post was imported from csharpfriends, if you have a similiar question please ask it again.
All previous members have been migrated, hope you enjoy the new platform!