Last Updated : 11 Jul, 2025
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.
Syntax of a PropertyTypes of Property Restrictions<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
C#
// Restriction on property with ref and out keyword
using System;
public class Property
{
public int MyProperty {
get;
set;
}
public void RefMethod(ref int value)
{
value = 10;
}
public void RefMethod(out int value)
{
value = 20;
}
}
public class Geeks
{
public static void Main(string[] args)
{
Property o = new Property();
}
}
Output:
Solution: Avoid passing properties as ref or out parameters. Use variables instead.
2. OverloadingRestriction: 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.
C#
// Demonstrates property overloading error
using System;
class Program
{
int num;
public int Number
{
get
{
return this.num;
}
set
{
this.num = value;
}
get
{
// Causes a compile time error
// which tells get accessor
return this.num;
} // has already been called
}
}
class Geeks
{
static void Main()
{
Program o = new Program();
o.Number = 5;
// If there is only one property in
// class obj, we will get the output:
Console.WriteLine(o.Number);
}
}
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
C#
// Properties in different classes
using System;
class Class1
{
int num;
public int Number
{
get
{
return this.num;
}
set
{
this.num = value;
}
}
}
class Class2
{
int num;
public int Number
{
get
{
return this.num;
}
set
{
this.num = value;
}
}
}
class Geeks
{
static void Main()
{
Class1 o1 = new Class1();
Class2 o2 = new Class2();
// Calls set mutator from the Class2 class
o2.Number = 7;
// Calls set mutator from the Class1 class
o1.Number = 5;
// Gets the get accessor form the Class1 class
Console.WriteLine("The value set in the class " +
"Class1 is: " + o1.Number);
// Gets the get accessor form the Class2 class
Console.WriteLine("The value set in the class " +
"Class2 is: " + o2.Number);
}
}
The value set in the class Class1 is: 5 The value set in the class Class2 is: 73. Immutability
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.
C#
// Immutability in Properties
using System;
class Class
{
int num;
public int Number
{
get
{
return this.num;
// here we get the warning
this.num = 5;
}
set
{
this.num = value;
}
}
}
class Geeks
{
static void Main()
{
Class o = new Class();
o.Number = 5;
Console.WriteLine(o.Number);
}
}
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#.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4