A property is a member that provides a flexible tool to read and write the value of a private field. We use them as public data members but actually, they are specific methods called accessors.
In this article, we are going to talk more about properties and how to use them in C#.
If you want to see complete navigation of this tutorial, you can do that here C# Intermediate Tutorial.
To download the source code, you can visit Properties in C# Source Code.
We are going to divide this article into the following sections:
- Property Syntax
- Read-Only and Write-Only Properties
- Property Accessibility
- Auto-Implemented Properties
- Conclusion
Property Syntax
The syntax of a property declaration can be used in the following way:
Access_Modifier Type PropertyName { get { //read actions } set { //write action } }
As we can see, a property can contain two blocks of code. The get block contains statements that execute when we read from a property. The set block contains statements that execute when we write to a property:
public class Student { private string _name; private string _lastName; public string Name { get { return _name; } set { _name = value; } } public string LastName { get { return _lastName; } set { _lastName = value; } } public Student(string name, string lastName) { _name = name; _lastName = lastName; } public string GetFullName() { return _name + ' ' + _lastName; } }
In the example above we see that our private fields are now exposed through the properties. If we want to read the value of the _name
field all we have to do is to call the Name
property with the student
object. The same applies to the _lastName
field. Moreover, if we want to set a value to our fields, all we have to do is to call a set block of our properties:
class Program { static void Main(string[] args) { Student student = new Student("John", "Doe"); string name = student.Name; //call to a get block of the Name property string lastName = student.LastName; // call to a get block of the LastName property student.Name = "David"; //call to a set block of the Name property student.LastName = "Dauni"; // call to a set block of the LastName property } }
Our properties can have a complex code inside get or set blocks. They are not limited only to read a value or just to write a value. We can use conditions or method calls etc. in the get or set blocks:
public int X { get { return _x; } set { _x = CheckValue(value); } } private int CheckValue(int val) { //code execution in here }
Read-Only and Write-Only Properties
We can declare a property that only has a get
block and not the set
. That kind of property is called Read-Only property. If we create a read-only property, we can only read the value of a private field. It is quite common to create a read-only property inside our class. What we want with it is to set it with the constructor method and then to use its value throughout the entire class, but never to set its value outside the constructor. If we try to set it, the compiler will throw an error:
public string Name { get { return _name; } }
In the same way, as we can create a read-only property, we can create a write-only property. That type of property has only the set block and not the get. It is not a common case to create write-only properties. Of course, if we need it, we can only set the values with this type of property and not read it:
public string Name { set { _name = value; } }
Property Accessibility
We can specify an access modifier for our property (public, private…) if we want to restrict its availability. But in C# we can even override the accessibility of get or set accessors. So, what we can do is declare a public property which has the public get accessor and private set accessor. If our property is a public one, we don’t have to add the public keyword for the get accessor, it is going to be public anyway:
public string Name { get { return _name; } private set { _name = value; } }
This means that we can read in all the classes from our Name property, but we can set it only within the Student
class.
When we use an accessor overriding inside the property, we must pay attention to the following rules:
- We can change the accessibility level of only one accessor. There is no point in having both accessors modified. If we want to modify both accessors, we should just modify the property access level.
- We can’t use access modifier on the get or set blocks that are less restrictive of the access modifier applied on a property itself. So, if our property is private, there is no point in having the public get or set block.
Auto-Implemented Properties
If no additional logic is required in a property accessor, we can use the auto-implemented properties for more readable and concise way of declaring properties. The auto-implemented property consists only of the get and set keywords, nothing more:
public string Name { get; set; } public string LastName { get; set; }
When we declare the properties like this, the compiler creates a private field for us, which could be accessed only through the property’s get or set accessors.
So in our example instead of:
private string _name; public string Name { get { return _name; } set { _name = value; } }
We can just write:
public string Name { get; set; }
In the Visual Studio, we are even going to get a suggestion to use an auto property:
Conclusion
Excellent.
In this article, we have learned:
- About properties and it’s syntax
- How to use read and write-only properties
- How to modify the accessibility level of the property
- The way to use auto-implemented properties
In the next article, we are going to talk about Static methods, static classes, and extension methods as well.