What is the set on Properties?

  • Thread starter Thread starter light_wt
  • Start date Start date
L

light_wt

Hi,

I have a simple question about methods property. The
syntax is
public <dataType> MtdName{
get {
// property get code
}
set {
// property set code
}
}


Correct me if I am wrong. The property can be assigned
without the set portion. So, what does the set exactly
do? May be readonly visibility for derived classes? Why
is the benifit to have a readonly property in a class?
Thanks.
 
OK, you're wrong.

The property cannot be assigned without the set. Without it the property is
readonly.

This should be enough information to answer the rest of your question.


HTH
Brian W
 
light_wt said:
Correct me if I am wrong. The property can be assigned
without the set portion.

That is incorrect. Without the set, you have a readonly property.
May be readonly visibility for derived classes?

*And* the base classs as well.
Why
is the benifit to have a readonly property in a class?

That's entirely up to the designer.

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
 
light_wt said:
I have a simple question about methods property. The
syntax is
public <dataType> MtdName{
get {
// property get code
}
set {
// property set code
}
}


Correct me if I am wrong. The property can be assigned
without the set portion. So, what does the set exactly
do? May be readonly visibility for derived classes? Why
is the benifit to have a readonly property in a class?
Thanks.

No, the property can't be assigned to without the set portion. The set
portion does whatever you tell it to do.

You can't currently give different accessibility to the get and set
portions, unfortunately.

The benefit of making a readonly property is that you don't need to
worry about users changing the values - it may not even make *sense* to
allow them to change them. For instance, what sense would it make to
allow a user to change the value of Encoding.ASCII, or Int32.MaxValue?

Immutable types are very handy - and they *must* be read-only, by
definition.
 
Back
Top