When I declare a variable in a class like this:
public class Test
{
public string x = 0;
}
and another like this:
public class Test2
{
public string x {get; set;}
}
What is the difference?
When I declare a variable in a class like this:
public class Test
{
public string x = 0;
}
and another like this:
public class Test2
{
public string x {get; set;}
}
What is the difference?
Usually neither called "variable". First one is "field", second one is "property" (auto-implemented property).
Excerpt from MSDN on "field":
A field is a variable of any type that is declared directly in a class or struct. Fields are members of their containing type.
Excerpt from MSDN on "property":
A property is a member that provides a flexible mechanism to read, write, or compute the value of a private field. Properties can be used as if they are public data members, but they are actually special methods called accessors. This enables data to be accessed easily and still helps promote the safety and flexibility of methods.