blah blah blah is here! blah blah » Close

up0down
link

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.

last answered 2 years ago

1 answers

up0down
link

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.

up0down
link

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

up0down
link

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.

up0down
link

Thank you, thanks a lot for your valuable reply,
Have a great time,
Anil

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!

Feedback