We can declare variables in the same way:
int myInt = 6;
string myString = "hello";
These are called
fields
When you use the get and set accessors, you are declaring it as a
property
which is a very different animal. A property is a a set of methods that are called when a variable is accessed - the get method is called when you fetch the value and the set method is called when you assign a value.
This is a lot more flexible: not only can you have a public getter so anyone can get the the value, you can add a private (or protected, or internal) setter to prevent changes outside your class.
You can also do anything within the get and set methods - you can build your string variable from a number of sources:
private string firstName;
private string secondName;
public string Name
{
get { return firstName + " " + secondName; }
set
{
string[] parts = value.Split(' ');
firstName = parts[0];
secondName = parts[1];
}
}
public string Email
{
get { return firstName + "." + secondName + "@myCompany.com";
}
This helps to keep the internals of your class away from the class that is using them.
[edit]Oops! Forgot to declare string array... :O - OriginalGriff[/edit]