Friday, 9 August 2013

What is the different between Value and Property? When to use? Why?

What is the different between Value and Property? When to use? Why?

Example 1:
class Class1
{
public static int A = 1;
}
Example 2:
class Class2
{
private static int _A = 1;
public static int A
{
get
{
return _A;
}
set
{
_A = value;
}
}
}
Assuming that I don't want to do any validation and perform any extra
functions. I just want to hold the plain data. Both Class1.A and Class2.A
have the same result. New number can be assigned to both too. So what is
the different for doing this? Is there any benefits? Why and When should I
use them?
If there is no difference between them, I should use Example 1, as Example
1 only requires 1 line of code and Example 2 requires 6-10 lines. Do you
agree?

No comments:

Post a Comment