Possible Duplicate:
What is the difference between a field and a property in C#?
i would like 开发者_JAVA百科to know the difference between a variable and a property.can any one explain
A property is actually a special function with a get method and a set method. The get and set methods can provide some logic, like validation that a field can not.
You can back your property with a private field, like this
private int age;
public int Age
{
get { return age; }
set
{
if (value < 0)
throw new ArgumentException("Must be > 0");
age = value;
}
}
You can also implement only the get part and calculate the result
private int a;
private int b;
public int Sum
{
return a + b;
}
Simply you can't validate the data being stored in variable but in a Property you can.
A Propery is more like a method as it has get
& set
methods which you define to handle the data.
Also they are usefull in Databinding.
Properties expose fields. Fields should (almost always) be kept private to a class and accessed via get and set properties. Properties provide a level of abstraction allowing you to change the fields while not affecting the external way they are accessed by the things that use your class.
Example:
like in case of Date of Birth. You need to validate that age is less then todays date:
In a Field this is valid:
DateTime age = new DateTime(2012,12,1);//1 december 2012
But in a property you can validate it:
private DateTime _dob;
public DateTime dob
{
get
{
if(_dob!= null) return _dob;
}
set
{
if(DateTime.Compare(value, DateTime.Today) <= 0)
{
_dob= value;
}
else
{
throw new System.InvalidOperationException("Date of Birth should be less then today's date");
}
}
}
A property or "auto property" is just an easier way to write the complete implementation. It's a way to make code more readable and more manageable.
A field
in C# inside your class outside of methods might look like this:
private object _obj;
However if you want to "expose" this to other classes that uses your class you don't just want to make it public
. Because that would not give you any control over what values it is set to.
Therefore you might want to have a getter
and a setter
for this particular field
.
One way to implement this, and a quite common way in most other languages is to have two methods:
Getter
public object getObj() { return _obj; }
Setter
public void setObj(object value) { _obj = value; }
However doing this for all of your fields
makes a lot of lines that you wouldn't really need, this is a trivial problem and therefore C# will help you out by giving you a way of defining "properties".
Properties in C# 1.0
In C# 1.0 you wrote a property like this:
private object _obj;
public object Obj
{
get { return _obj; }
set { _obj = value; }
}
You still needed the private field
though, which is still an overhead.
Properties in C# 2.0 and onwards
In later versions of C# you could make it easier for yourself, in C# 2.0 you could write something like this:
public object Obj { get; set; }
Which would auto-generate the above in your IL
which in the end would end up looking the same as the first example when using methods.
In C# 3.0 an issue was solved, how do you create a public getter but a private setter?
Nowdays you can write it like this:
public object Obj { get; private set; }
Which will let you expose the object, but make it read only! ( not true read only since you can manipulate it inside your class ).
So simply explained, a field is a variable exposed inside or outside your class, but you have no way of controlling the values that someone with direct access to it decides to set it to.
A property is a way to expose setters / getters to your fields, making it easier to manage input / output to and from your field.
精彩评论