G
Gary Morris
Hello all,
OK, first of all I have known about properties since VB6, which
I have and have used extensively. It seems that property get
and set are basically the same concept in C# and VB.NET, so
no problem there. In programming with VB6, I never wrote any
program that used properties, and to date I still haven't ever
used them in C#. What I really want to know is, when should I
use them at all? I mean, we have variables (now referred to as
fields, I think), which can be made public or private, but all the
time I see code which:
- first declares a field as private
- then uses a public property to read or write to the private field
Now, I can understand how they would be really handy when
you need a read-only field, but for the life of me I cannot see the
point in creating yet another member for a class that does
nothing except assign/read a value to/from a private field that
could have just been made public to begin with. I also want to
state that I can see a good use for them if you wanted to DO
something to the field after it's read or written, and that's the
only use I can see for properties. After all, a public field is
accessed from code outside it's containing class just like a
property is, so why bother with something like:
private int MyInt;
public int MyIntP
{
get
{
return MyInt;
}
set
{
MyInt = value;
}
}
Now, if the set part went like this:
......
set
{
MyInt = value * 2;
}
......
I could understand that with no problem.
It just seems to me that it does exactly the same thing as a normal
public field does. I would like to hear an explanation as to why this
is used so often when the property does nothing to the private field
except to read/write its value. If I'm missing something really
important here, I want to know about it, but if not, I also want to
know why so many people use them (constantly) if there is not a
legitimate need.
Thanks.
OK, first of all I have known about properties since VB6, which
I have and have used extensively. It seems that property get
and set are basically the same concept in C# and VB.NET, so
no problem there. In programming with VB6, I never wrote any
program that used properties, and to date I still haven't ever
used them in C#. What I really want to know is, when should I
use them at all? I mean, we have variables (now referred to as
fields, I think), which can be made public or private, but all the
time I see code which:
- first declares a field as private
- then uses a public property to read or write to the private field
Now, I can understand how they would be really handy when
you need a read-only field, but for the life of me I cannot see the
point in creating yet another member for a class that does
nothing except assign/read a value to/from a private field that
could have just been made public to begin with. I also want to
state that I can see a good use for them if you wanted to DO
something to the field after it's read or written, and that's the
only use I can see for properties. After all, a public field is
accessed from code outside it's containing class just like a
property is, so why bother with something like:
private int MyInt;
public int MyIntP
{
get
{
return MyInt;
}
set
{
MyInt = value;
}
}
Now, if the set part went like this:
......
set
{
MyInt = value * 2;
}
......
I could understand that with no problem.
It just seems to me that it does exactly the same thing as a normal
public field does. I would like to hear an explanation as to why this
is used so often when the property does nothing to the private field
except to read/write its value. If I'm missing something really
important here, I want to know about it, but if not, I also want to
know why so many people use them (constantly) if there is not a
legitimate need.
Thanks.