I've actually never seen protection level used there, usually I see the protection level set where "public" is in your example and protected there would mean that only classes that inherit from this class could access the "somename" variable. I'm going
to guess that perhaps you can use this technique to add further protection to specific actions, so perhaps if you did this:
public string somename{ get; protected set; }
means that the "get" accessibility is public (presumably because it inherits from the "public" identifier, but the "set" accessibility is protected which could make sense if you wanted other classes to have read-only access (I slightly modified your example
because read-only is common, whereas write-only is odd), but classes that extend this class to have write-access also.
[EDIT]
After playing around with it a little, it looks as if my assumptions are pretty close. You can't, for instance, establish an inner accessibility if there is only one get or set. However, once you have both, then you can declare one or both with a different
accessibility, which suggests that if you don't establish an accessibility, it'll simply inherit it from the outer-accessibility declaration.
[/EDIT]
"Dream as if you'll live forever, live as if you'll die today." --James Dean
Thanks, I'm just playing around with some code and as it never through an error and I could still access the values I was just wondering if its ever used.
Thanks
George
Made redundant and now looking to build website that i always wanted to. (I'm not a professional programmer, just teaching myself)
No doubt. Your curiosity in situations like this will make you a better developer because you don't just aim to make it work, you aim to understand why something does or doesn't work.
"Dream as if you'll live forever, live as if you'll die today." --James Dean
CareerChange
Member
43 Points
180 Posts
When would you use protected get
Jan 30, 2013 09:08 PM|LINK
Hi
Can anyone please tell me when you would ever use the code below.
public string somename{ protected get; set; }
I'm just curious as to when it would ever be used.
Thanks
George
AceCorban
Star
12318 Points
2269 Posts
Re: When would you use protected get
Jan 30, 2013 10:28 PM|LINK
I've actually never seen protection level used there, usually I see the protection level set where "public" is in your example and protected there would mean that only classes that inherit from this class could access the "somename" variable. I'm going to guess that perhaps you can use this technique to add further protection to specific actions, so perhaps if you did this:
public string somename{ get; protected set; }means that the "get" accessibility is public (presumably because it inherits from the "public" identifier, but the "set" accessibility is protected which could make sense if you wanted other classes to have read-only access (I slightly modified your example because read-only is common, whereas write-only is odd), but classes that extend this class to have write-access also.
[EDIT]
After playing around with it a little, it looks as if my assumptions are pretty close. You can't, for instance, establish an inner accessibility if there is only one get or set. However, once you have both, then you can declare one or both with a different accessibility, which suggests that if you don't establish an accessibility, it'll simply inherit it from the outer-accessibility declaration.
[/EDIT]
CareerChange
Member
43 Points
180 Posts
Re: When would you use protected get
Jan 30, 2013 10:49 PM|LINK
Hi AceCorban
Thanks, I'm just playing around with some code and as it never through an error and I could still access the values I was just wondering if its ever used.
Thanks
George
AceCorban
Star
12318 Points
2269 Posts
Re: When would you use protected get
Jan 30, 2013 11:16 PM|LINK
No doubt. Your curiosity in situations like this will make you a better developer because you don't just aim to make it work, you aim to understand why something does or doesn't work.