In c#, Property is an extension of the class variable. It provides a mechanism to read, write, or change the class variable's value without affecting the external way of accessing it in our applications.
In c#, properties can contain one or two code blocks called accessors, and those are called a get
accessor and set
accessor. By using get
and set
accessors, we can change the internal implementation of class variables and expose it without affecting the external way of accessing it based on our requirements.
Generally, in object-oriented programming languages like c# you need to define fields as private and then use properties to access their values in a public way with get
and set
accessors.
Following is the syntax of defining a property with get
and set
accessor in c# programming language.
If you observe the above syntax, we used an access modifier and return type to define a property along with get
and set
accessors to make required modifications to the class variables based on our requirements.
Here, the get
accessor code block will be executed whenever the property is read, and the code block of set
accessor will be executed whenever the property is assigned to a new value.
In c#, the properties are categorized into three types, those are.
Type | Description |
---|---|
Read-Write | A property that contains a both get and set accessors, then we will call it a read-write property. |
Read-Only | A property that contains only get accessor, then we will call it a read-only property. |
Write-Only | A property that contains only set accessor, then we will call it a write-only property. |
In c#, Properties won’t accept any parameters, and we should not pass a property as a ref or out parameter in our application.
Following is the simple example of defining a private variable and a property in the c# programming language.
If you observe the above example, we defined a property called “Name” and we used a get
accessor to return a property value and set
accessors to set a new value. Here, the value keyword in set
accessor is used to define a value that is being assigned by set
accessor.
In c#, the get
accessor needs to be used only to return the field value or to compute it and return it, but we should not use it for changing the state of an object.
As discussed, we can extend the behavior of class variables using properties get
and set
accessors. Following is the example of extending the behavior of private variable in property using get
and set
accessors in c# programming language.
If you observe the above example, we are extending the behavior of private variable name using a property called Name with get
and set
accessors by performing some validations like to make sure Name value equals to only “Suresh” using set
accessor and converting property text to uppercase with get accessor.
Here the field “name” is marked as private, so if you want to make any changes to this field, we can do it only by calling the property (Name).
In c# properties, the get
accessor will be invoked while reading the value of a property, and when we assign a new value to the property, then the set
accessor will be invoked by using an argument that provides the new value.
Following is the example of invoking get
and set
accessors of properties in c# programming language.
In the above example, when we assign a new value to the property, then the set
accessor will be invoked and the get
accessor will be invoked when we try to read the value from the property.
Following is the example of defining properties with get
and set
accessors to implement required validations without affecting the external way of using it in the c# programming language.
If you observe the above example, we are extending the behavior of private variables (name, location) using properties (Name, Location) with get
and set
accessors by performing some validations like to make sure Name value equals to only “Suresh” using set
accessor and converting property text to uppercase with get
accessor.
When you execute the above c# program, we will get the result as shown below.
If you observe the above example, our variable text converted to upper case, and even after we set variable text as “Rohini”, it displayed text as “Suresh Dasari” because of the set
accessor validation fails in the property.
As discussed, if a property contains the only get
accessor, then we will call it a read-only property. Following is the example of creating read-only properties in the c# programming language.
If you observe the above example, we created properties using only get
accessor to make the properties are read-only based on our requirements.
If we uncomment the commented code, we will get a compile error because our Name property doesn’t contain any set
accessor to set a new value. It’s just read-only property.
When you execute the above c# program, you will get a result like as shown below.
This is how we can create read-only properties in c# applications based on our requirements.
As discussed, if a property contains the only set
accessor, then we will call it a write-only property. Following is the example of creating write-only properties in the c# programming language.
If you observe the above example, we created properties using only set
accessor to make the properties are write-only based on our requirements.
If we uncomment the commented code, then we will get a compile error because our Name property doesn’t contain any get
accessor to return a value. It’s a just write-only property.
When you execute the above c# program, you will get a result like as shown below.
This is how we can create write-only properties in c# applications based on our requirements.
In c#, a property is called an auto-implemented property when it contains accessors (get, set) without having any logic implementation.
Generally, the auto-implemented properties are useful whenever there is no logic implementation required in property accessors.
Following is the example of creating auto-implemented properties in the c# programming language.
If you observe the above example, we created properties with get
and set
accessors without having any logic implementation.
When you execute the above c# program, we will get a result like as shown below.
This is how we can create auto-implemented properties in c# applications based on our requirements.
The following are the important points which we need to remember about properties in the c# programming language.
get
and set
accessors by hiding implementation details.get
accessor is used to return a property value and a set
accessor is used to assign a new value.set
accessor is used to define a value that is going to be assigned by the set
accessor.