Behind the Scenes Look at C#: Properties - C# Properties
(Page 3 of 4 )
As you know, accessing public fields from the client code can be very dangerous, and programmers used to define get and set methods to have access to private fields. C# defines a construction called a property, which has been designed to have access to the private field. There are two ways to look at a property of a class.
The first way is to look at the property as an attribute of the class, like the class Employee which can define a property called FirstName, and another one called Age; the list is not limited. So instead of considering the fields as the class attributes, we will declare them as private fields and declare the properties as public to have indirect access to the fields. The other way to look at properties is from the MSIL code. When you define a property in your class, the C# compiler generates two methods, which are a get method and a set method to form the property, as we will see shortly.
You access the property in the same way as you access the field, and you assign values to the property in the same way you assign values to the fields (using the assignment operators). You can think of the property as a public representation of your private field, and deal with it on that basis. The following code is the syntax for writing a property:
public class Customer
{
private string firstName;
public string FirstName
{
get
{
return this.firstName;
}
set
{
this.firstName = value;
}
}
}
The class Customer defines the private field firstName of type string and defines a public property that represents the field. In other words, it defines a property to access the field. As you can see, the property declaration consists of an access modifier followed by the data type of the property (which must be the same as the field), followed by the name of the property. Note that we don't use parentheses with properties, because we don't pass any parameters. The property defines a get accessor that returns the value of the private fields (which is much more like our Getxxx method), and defines a set accessor that assigns a value to the private field.
The very interesting part here is when the get and set accessors are called. When you write an expression like aCustomer.FirstName = "Michael" the C# compiler understands that you need to assign this value, calls the set part of the property, and passes this value "Michael" to it. Note that inside the set accessor you refer to the assigned value using the keyword value. When you write an expression like Console.WriteLine(ac.FirstName), the C# compiler understands that you need to get (or read) the private field value, so it calls the get part of the property.
When you write a property that contains only the get accessor, you are defining a Read-Only property. The .NET Framework Classes define many Read-Only property, like the one that we just met in the above application: the Exception.Message property is a Read-Only property that gets a message representing the exception object. This is very useful, because sometimes you need to disallow the assignment to the property.
When you declare a property only with the set accessor, you are defining a Write-Only accessor. It's not very common to do so; anyway, it exists, and who knows? You may find a scenario that uses a Write-Only accessor; maybe you have a highly secure system that doesn't have a "forget your password" process because it defines the Password property as Write-Only.
The Customer class contains (actually it must contain) a private field to store the value that will be set by and get by the property. Note that we have used the Pascal Naming Convention to name the property FirstName and the camel Naming Convention to name the private field firstName, because C# is case sensitive and both the C# compiler and the Runtime know that firstName is not the same as FirstName. So inside the class Customer we can use the identifier firstName to the private field, and outside the class we use the property name (the public name of the field) FirstName. This is perfect to express what we are talking about when we say "a public name of the field". Most of the time you will define your properties as public, but many times you will define some of them as protected, so the derived classes can access them but the client code can't. You can define a property as private, but it's very uncommon to do so.
The following is the client code that access (set and get) the property.
{
Customer ac = new Customer();
ac.FirstName = "Michael";
Console.WriteLine(ac.FirstName);
}
As you can see, the client code has no access to the field firstName, and the access to the field can only happen through the FirstName property. Here we use the same coding technique as we have used with the public fields example; we assign the value through the assignment operator. The coding technique of the Getxxx/Setxxx methods is not perfect because of the passing of the values to the methods. The assignment technique makes you feel like you are assigning to the field itself.
Next: Modifying the Worker Class >>
More C# Articles
More By Michael Youssef