Restrictions of properties in C#

Properties look, act and feel like fields, but they aren’t true fields and certain restrictions apply to them:
• You can’t initialize a property of a struct through a set accessor unlike field being initialized with an assignment!

• You can’t use a property as a ref or out argument.

• You can’t declare multiple properties in a single declaration statement.

• You can’t declare constant or read only properties.

• You can declare only a get or set accessor, or both, inside a property.

• You can’t declare a property whose type is void.

• You can’t declare a property without a parameter before the property name!

• You can’t declare a property with two more parameters before the property name.

Using static properties
You can declare 2 kinds of fields in a class or struct:

1. Instance fields (one per object) 2. static fields (one per type). You can use property to copy them. The static property is exactly the same as for an instance property with static keyword prefix the property type/parameter:

    struct ScreenPos
    {
    private int x, y;
    private static ScreenPos vgaLimits = new ScreenPos(600, 800);
    private static ScreenPos limits = vgaLimits;
    
    public static ScreenPos Limits    	//static property
    {
    	get { return limits; }
    }
   
   private static int rangeCheckX(int x)
   {
   	if (x < 0 || x > limits.x)
   	{
   		throw new ArgumentOutOfRangeException(“X”);
   	}
   	return x;
   }
   
   private static int rangeCheckY(int y)
   {
   	if (y < 0 || y > limits.y)
   	{
   		throw new ArgumentOutOfRangeException(“Y”);
   	}
   	return y;
   }
   }
You can access a static property by using the name of its enclosing struct or class:
Console.WriteLine (ScreenPos.Limits);
Declaring interface property:
You can also declare properties in an interface:
    interface IScreenPos
    {
    	int X{get;set;}
    	int Y{get;set;}
    }
Any class or struct that implements this interface must implement the accessors:
   struct ScreenPos : IScreenPos
   {
    	private int x, y;   
   	public int X
   	{
   		get { return x; }
   		set { x = rangeCheckX(value); }
   	}
   
   	public int Y
   	{
   		get { return y; }
   		set { y = rangeCheckY(value); }
   	}
   }
If you implement the interface property in a class, You can declare the property implementations as virtual, which allows further derived classes to override the implementations of properties:
   class ScreenPos : IScreenPos
   {
    	private int x, y;    
    	public virtual int X
    	{
    		get { return x; }
    		set { x = value; }
    	}
   
   	public virtual int Y
   	{
   		get { return y; }
   		set { y = value; }
   	}
   }
Note: The virtual keyword isn’t valid in struct because you can’t derive from struct; struct is implicitly sealed!
You can also choose to implement a property by using the explicit interface implementation syntax which is non-public and non-virtual and can’t be overridden:
   struct ScreenPos : IScreenPos
    {
    	private int x, y;    
    	int IScreenPos.X
    	{
    		get 
{ 
return x; 
}
    		set 
{ 
x = rangeCheckX(value); 
}
   	 }
   
   	int IScreenPos.Y
   	{
   		get 
{ 
return y; 
}
   		set 
{ 
y = rangeCheckY(value); 
}
   	}
   }
Tip: you can declare interfaces as properties that are read-only (no set) or write-only (no get). You can even split the get and set accessors across different interfaces.
Scroll to Top