Very easy one to explain. 
Imagine you have some functionality in your program, say a private List<string> that holds a random set of special data, and you want to give access to only 1 piece of that List<string>.
You could easily just do:
Code:
public string MyString { get { return myList[0]; } }
That is a common usage scenario. Keep in mind, anything you can normally put in a method (i.e: GetMyString()
you can do in a property instead.
Another very useful thing about properties, is the encapsulation they provide.
Imagine the following code:
Code:
private string randomName = string.Empty;
public string RandomName
{
get
{
if (string.IsNullOrEmpty(randomName))
{
throw new NullReferenceException("Random name is null or empty!");
}
return randomName;
}
set
{
if (randomName != value)
{
if (!string.IsNullOrEmpty(value)
{
randomName = value;
}
}
}
}
This allows methods within your class to act on the randomName field no matter what is contained in it. While only allowing methods outside of the class to pass valid info, and throw an error when it's trying to retrieve a null or empty string. When using the following:
Code:
myClassInstance.RanomName = "";
It will not change randomName as it has not passed the checks in the setter of the RandomName property. (Check if the value passed in is not the same as what is already set, and make sure the string is not null or empty)
If you were to retrieve RandomName without ever changing the randomName field from string.Empty, you would have an exception thrown.
The possibilities are endless. (You can let properties set certain settings throughout your program, do many different checks, etc.)
The most common usage is just
Code:
public string RandomName { get; set; }
Which acts just like a field would, but with a different intellisense glyph. (The compiler adds the backing field for you) Properties can be very, very powerful.
For instance:
Code:
public string RandomName { get; protected set; }
This would allow any other class to get the data from the RandomName property, but only classes that are derived, or are, the current class.
Code:
public class MyClass
{
public string RandomName { get; protected set; }
void Set()
{
RandomName = "This works!";
}
}
public class OtherClass
{
void Set()
{
(new MyClass()).RandomName = "This will not work! Cannot set a protected property if this class does not derive from MyClass!";
}
}
public class MyDerivedClass : MyClass
{
void Set()
{
RandomName = "This works too! We're derived from MyClass, and thus have 'protected' privileges";
}
}
The same can be done with getters.
Code:
public class MyClass
{
// Allow only derived types to set this property. All others can get it.
public string RandomName { get; protected set; }
// Allow only derived types to get this property. All others can set it.
public string OtherName { protected get; set; }
// Allow only class instance types to get this property, all others can set it.
public string AnotherName { private get; set; }
// Allow only assembly internal types to get this property. Allow only class instance types can set it.
public string ConfusingName { internal get; private set; }
// Same as above. Just a bit less confusing.
internal string ConfusingName2 { get; private set; }
}
Hope you understand it now.