![]() |
VOOZH | about |
Properties in C# are special class members that provide a flexible mechanism to read, write, or compute the value of a private field. They act as methods called accessors i.e. "get" and "set" methods. Using Properties, we can achieve encapsulation and information hiding. Properties allow controlled access to the fields, ensuring data integrity while maintaining simplicity.
Despite their flexibility, properties in C# have certain restrictions and limitations. In this article, we will learn all these restrictions in detail.
<access_modifier> <return_type> <property_name>
{
get { // body }
set { // body }
}
We have to follow a few rules or restrictions while writing properties, which are as follows:
Restriction: A property is restricted from being passed via the ref or out parameter to a method. Properties are methods and both ref and out are not considered part of the 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.
Example: Property Restriction with the ref and out
Output:
Solution: Avoid passing properties as ref or out parameters. Use variables instead.
Restriction: C# does not allow overloading of properties. Each property can have only one get and one set accessor. If we attempt to define multiple accessors with the same name, it will lead to a compilation error. It means that we can only put a single get (accessor) and set (mutator) in a class. This restriction ensures consistency. Because multiple accessors could lead to confusion about which method should be used, leading to unpredictable behaviour.
Example: The program given below shows what happens when we give more than one accessor in the same class.
Output:
Explanation: The property "Number" cannot have multiple get or set accessors in the same class.
But by using properties in different classes in the same program, one can use the get and set multiple times.
Example: Creating Multiple Accessors by using Different Classes in the Same Program
The value set in the class Class1 is: 5 The value set in the class Class2 is: 7
Restriction: 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 will get a warning which tells us that the code we are trying to access is unreachable.
Example: Demonstration of property can not alter the state of the underlying variable when the get accessor is called.
Output:
Explanation: 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 in changing the underlying variable when the get accessor is called. Which shows the immutability of properties in C#.