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:

Using properties in a class is as easy as this:

Now, we can easily use our class in a program, as follows:

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:

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:

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:

Then, you would be able to use the class in a program, as follows:

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.

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?

Share

Comments are closed.

Post Navigation