Hi All,
What is the advantage of .NET properties? For instance, I often see something like this:
public class donut
{
private string _filling;
public stringfilling{
get {
return _filling;
}
set {
_filling= value;
}
}
Now, traditionally in other OOP such as C++ or Java,you would have accessor methods (i.e., getter and setter methods, thatworked something like this:
public class donut
{
private string filling;
public voidgetFilling(){
return _filling;
}
public string setFilling(string newFilling) {
filling = newFilling;
}
}
Is there a difference between these twomethods? What is the reason to use .NET properties overgetter/setter methods? When I moved to .NET I was usinggetter/setter methods until I discovered properties, but I found myselfconfused as to the reason for them.
It's just a matter of syntax really. Use whatever mechanism your chosen language has given you.
But it seems like the get/set method would have worked fine in .NET(regardless of language). For some reason, Microsoft decided toadd another way of doing this.
I have to figure that if they added a new way of doing it, they musthave thought the "old" way had some kind of deficiency. Otherwise, why add a new syntax, right?
So is there a reason for the new way?
Hi there,
Well, my personal opinion is that is more practical. Instead of having 2 functions you have 1 property.
The functionality is the same. However, when you use other components of the .Net framework, properties come in handy. For example, a datagrid will bind an array of objects and will use the properties to bind.
Is just a different way to do the getter/setter. But is kind of enforce by the use that has on the rest of the framework.
Hope this helps,
Covo
0 comments:
Post a Comment