Introduction
When coding, I find that properties are a simple, yet useful, feature of the C# programming language. Properties enable the use of member syntax for setters and getters. With properties, you can read, write, or compute the value of a private field very flexibly. If you have used C++ or Java in the past, you most likely have seen methods for reading and writing member variables in a class. For example, the combination of setLength(int length) and getLength() would respectively allow for the setting and getting of some length value. In C#, special methods like this are called accessors.
In this article, you will see how properties can be used in C#. You will also see some use cases that demonstrate where it might be a good place to use properties.
Defining
Here is some simple syntax for defining properties in a class:
1 2 3 4 5 |
public <return type> <name> { get { } // This is the "getter" set { } // This is the "setter" } |
Using properties in a class is as easy as this:
1 2 3 4 5 6 7 8 9 |
class Rect { private int _length; public int length { get { return _length; } set { _length = value; } // Here, "value" represents any data of the same type as "length". (In this case, int) } } |
Now, we can easily use our class in a program, as follows:
1 2 3 4 5 6 7 8 9 |
class Properties { public static void Main() { Rect rect = new Rect(); rect.length = 20; // Set length to 20 System.Console.WriteLine(rect.length); // Retrieve length } } |
As you can see, it is relatively easy to use this feature in C#. But what good is this feature in the real world?
Use Cases
Properties are most useful wherever you need to implement member syntax for getting or setting data from an array or struct. Without properties, you would have to resort to implementing your own get and set methods. For instance, suppose you have the following class “Vector3f” for holding the three members x, y, and z:
1 2 3 4 |
class Vector3f { public float x, y, z; } |
However, suppose you want to store the values of the vector in an array instead. (This could happen for a number of reasons; maybe you want to perform special operations that can be done only on arrays, such as SIMD operations for efficiency, etc.) Suppose you also want to implement the methods to set and get the x, y and z values from an array. Without the properties feature, the class would look like the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
class Vector3f { public float[3] vec; public float GetX() { return vec[0]; } public float GetY() { return vec[1]; } public float GetZ() { return vec[2]; } } |
With properties, you could simply implement the setters and getters for the x, y and z values as properties. This would be done as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
class Vector3f { public float[3] vec; public float x { get { return vec[0]; } set { vec[0] = value; } } public float y { get { return vec[1]; } set { vec[1] = value; } } public float z { get { return vec[2]; } set { vec[2] = value; } } } |
Then, you would be able to use the class in a program, as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
class Properties { public static void Main() { Vector3f vec = new Vector3f(); vec.x = 20.1; vec.y = 40.2; vec.z = 10.5; Console.WriteLine(vec.x); Console.WriteLine(vec.y); Console.WriteLine(vec.z); } } |
Another example of where properties may be useful is when you want to implement something in setter/getter, yet you want to use properties as if you are accessing a variable instead. This article at MSDN provides a great example of a class called TimePeriod, which stores a time period in seconds. In this example, a property called Hours specifies a time in hours, and performs the conversion between hours and seconds.
1 2 3 4 5 6 7 8 9 |
class TimePeriod { private double seconds; public double Hours { get { return seconds / 3600; } set { seconds = value * 3600; } } } |
Properties can also be used to make a variable read-only. For instance, arrays in C# have a property called Length, which gives the size of the array. It can only be read from, not written to.
Conclusion
I find properties as one of the “killer” features of C#, because it makes almost all libraries easier to use and develop. This is one reason why C# is a good choice for developing libraries; no other statically-typed language supports this feature (although the “D” language also has properties feature… but I digress.) What is your opinion on this feature?