Prerequisite: Properties in C#
Properties are the special type of class members that provides a flexible mechanism to read, write, or compute the value of a private field. Properties can be used as if they are public data members, but they are actually special methods called accessors. This enables data to be accessed easily and help to promote the flexibility and safety of methods. Encapsulation and hiding of information can also be achieved using properties. It uses pre-define methods which are “get” and “set” methods which help to access and modify the properties.
Syntax:
<access_modifier> <return_type> <property_name>
{
get { // body }
set { // body }
}
Restrictions on Properties: We have to follow a few rules or restrictions while writing properties which are as follows:
- A property cannot be passed via ref or out parameter to a method: Properties cannot be passed via out or ref, as properties, are actually methods. Both ref and out are not considered to be a part of method signature at the time of compilation. Due to this, it will give a compile-time error. There is no solution to this restriction except to remove ref or out from the program entirely.
- You cannot overload a property: A property cannot be overloaded. It means that one can only put a single get and set accessor and mutator in a class respectively. The program given below shows what happens when we give more than one get accessor in the same class.
// C# program to show what happens when// we try to overload a propertyusingSystem;classMyprop {int_number;publicintNumber{get{returnthis._number;}set{this._number = value;}get{// Causes a compile time error// which tells get accessorreturnthis._number;}// has already been called.}}// Driver ClassclassGFG {// Main MethodstaticvoidMain(){Myprop ex =newMyprop();// set { }ex.Number = 5;console.WriteLine("If there are only two properties"+" in class ex we will get the output");// get { }Console.WriteLine(ex.Number);}}chevron_rightfilter_noneCompile Time Error:
prog.cs(19,3): error CS1007: Property accessor already defined
But by using properties in different classes in the same program, one can use the get and set multiple times. An example illustrating this is given below.
// C# program to demonstrate the use// of properties in different classesusingSystem;classMyprop {int_number;publicintNumber{get{returnthis._number;}set{this._number = value;}}}classMyprops {int_number;publicintNumber{get{returnthis._number;}set{this._number = value;}}}// Driver ClassclassGFG {// Main MethodstaticvoidMain(){Myprop ex =newMyprop();Myprops exs =newMyprops();// Calls set mutator from the Myprops classexs.Number = 7;// Calls set mutator from the Myprop classex.Number = 5;// Gets the get accessor form the Myprop classConsole.WriteLine("The value set in the class "+"Myprop is: "+ ex.Number);// Gets the get accessor form the Myprops classConsole.WriteLine("The value set in the class"+" Myprops is: "+ exs.Number);}}chevron_rightfilter_noneOutput:
The value set in the class Myprop is: 5 The value set in the class Myprops is: 7
- A property should not alter the state of the underlying variable when the get accessor is called: The get accessor of properties is preset to read-only mode while the set command is set to write-only mode. Due to this, if we try to enter values or modify in the scope of the get accessor we’ll get a warning which tells that the code we are trying to access is unreachable. The example given below illustrates that.
// C# program to demonstrate the// above-mentioned restrictionusingSystem;classMyprop {int_number;publicintNumber{get{returnthis._number;// We get the warning saying that// the code is unreachable.this._number = 5;}set{this._number = value;}}}// Driver ClassclassGFG {staticvoidMain(){Myprop ex =newMyprop();// set { }ex.Number = 5;// get { }Console.WriteLine(ex.Number);}}chevron_rightfilter_noneWarnings:
prog.cs(16,4): warning CS0162: Unreachable code detected
Output:
5
As seen by the output of the program, the code is run successfully but the values that we add in the get accessor cannot be used as it is unreachable. That is why there is no use of changing the underlying variable when the get accessor is called.
Recommended Posts:
- How to merge properties of two JavaScript objects dynamically?
- C# | Properties
- HTML | DOM Window opener Properties
- HTML | DOM Window frames Properties
- Transition shorthand with multiple properties in CSS?
- HTML | DOM Window frameElement Properties
- HTML | DOM Window localStorage Properties
- HTML | DOM Window defaultStatus Properties
- JavaScript | Object Properties
- How to get a subset of a javascript object's properties?
- How to override the CSS properties of a class using another CSS class ?
- Delegated Properties in Kotlin
- Difference Between Properties and Indexers in C#
- How to set default values for Angular 2 component properties?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Akanksha_Rai

