Possible Duplicate:
What is the difference between a field and a property in C#?
Difference between Property and Field in C# .NET 3.5+
I have seen that in c# the following pattern is common:
private string m_name = string.Empty;
public string Name
{
get
{
return m_name;
}
set
{
m_name = value;
}
}
why do we use a field and a property to hold a value when we can use a simple variable?
why should I use fields and properties instead of a simple variable?
Just for encapsulation principle. For hinding concrete implementaiton, and plus, you have an opportunity (in this case) to add additional code inside
get/set.If you don’t need addittional code, you can use just
or use fileds, as you would like. But using properties is a guideline offered by Microsoft.
So basically all, follow it.