Behind the Scenes Look at C#: Properties
(Page 1 of 4 )
Without properties you define your fields as public (not private). By doing that, you give client code access to the fields and allow it to modify the values as much as they want. In this article we will discuss coding techniques without properties, with properties, and more.
The Public Fields Example
In the previous articles we have used public fields to store values, but this is not a good Object Oriented Programming practice. Let's see why. Compile the following code and run it:
using System;
namespace Properties
{
class Class1
{
static void Main(string[] args)
{
// this code is fine
Worker worker1 = new Worker();
worker1.FirstName = "Michael";
worker1.LastName = "Youssef";
worker1.Department = "IT";
worker1.Age = 22;
// this code is not fine
Worker worker2 = new Worker();
worker2.FirstName = "Steve";
worker2.LastName = "John";
worker2.Department = "NONE";
worker2.Age = 900;
Console.WriteLine(worker1);
Console.WriteLine(worker2);
Console.ReadLine();
}
}
public class Worker
{
public string FirstName;
public string LastName;
public string Department;
public int Age;
public override string ToString()
{
return FirstName +" "+LastName+" is working in "+Department+
" and he's "+Age+" years old";
}
}
}
You will get the following result to the console. Do you like it?

Steven John is working in NONE and he's 900 years old. As you can tell, the object worker2 has invalid data and its state is not maintained. The client code (our Main method) has accessed the public fields of the object and assigned values to the fields which yield us this mess.
We need a way to secure our data, so the first thing that comes to our mind is to declare these fields as private and use methods to set and get the values. Using this technique, we can write code to check the user input (suppose that the value 900 has been input from the user) and accept only the data that is valid to the object and to our business rules. For example, you have a business rule that states that the worker age must be less than 40 years old on the day of hiring, and you have another rule that states that the worker nationality is American because you don't hire international workers, and so on.
The method that returns or gets the value of the private field is called a get method. By convention programmers used to call it a get accessor because this method has access to a private field, and it's the only way to get this private field's value. The method that sets or assigns a value to private fields is called a set method. Programmers used to call it a set accessor because it has code that sets a value to the private field, and maybe after some validation code, checks that the data is valid for this field.
By convention, programmers used to name the get methods with the prefix "Get." If you have a private field called firstName, it's better to call the get method GetFirstName(). The same holds true with the set methods, so the set method for the firstName field is SetFirstName() and so on.
Let's modify the above example to secure the object's fields.
Next: The Private Fields Example >>
More C# Articles
More By Michael Youssef