J
JB
I was always taught in C++ that using public variables was a bad idea,
which i could understand, so some generic get/set methods were usually
wrote to access the data.
Now in C# I'm just curious about properties.
Say I have
class SomeClass
{
private in data
public int Data()
{
get {return data;}
set {data=value;}
}
vs
class SomeClass
{
public int data
}
Most seem to prefer using the properties approach, but not yet being a
pro at C# i'm struggling to find a difference.
In effect they both seem exactly the same.
I can see how using properties would be useful if you need to apply
some sort of extra processing before setting the value, or for
creating read-only/write-only variables.
But for the very basic, is there any reason i should be using
properties instead of public variables?
In fact for the basic is there any difference in execution time
between get/set and direct access?
which i could understand, so some generic get/set methods were usually
wrote to access the data.
Now in C# I'm just curious about properties.
Say I have
class SomeClass
{
private in data
public int Data()
{
get {return data;}
set {data=value;}
}
vs
class SomeClass
{
public int data
}
Most seem to prefer using the properties approach, but not yet being a
pro at C# i'm struggling to find a difference.
In effect they both seem exactly the same.
I can see how using properties would be useful if you need to apply
some sort of extra processing before setting the value, or for
creating read-only/write-only variables.
But for the very basic, is there any reason i should be using
properties instead of public variables?
In fact for the basic is there any difference in execution time
between get/set and direct access?