Properties in C#

Properties allow you to control the accessibility of class variables and are the optional way to access variables from the outside in an object oriented programming language like C#. A property is much like a combination of a variable and a method – it can’t take any parameters, but you are able to process the value before it’s assigned to our returned. A property consists of 2 parts, a ‘get’ and a ‘set’ method.
Example
Note: In the following example, Lowercase x and y are private fields and Uppercase X and Y are public field-like properties. X and Y aren’t method and don’t have parentheses. They contain only a get and set accessor.
    struct ScreenPos
    {
        	//Declare the x and y coordinate of screen
private int x, y;
     	//RangeChecked method for x and y coordinates of the screen
         	public ScreenPos(int X, int Y)
        	{
            	 x = rangeCheckX(X);
             	y = rangeCheckY(Y);
        	}
    	//Setting property for x coordinates of the screen
        	public int X
        	{
           		//get Property
 get
            	{
               		 return x;
            	}
            	//set Property
set
            	{
                		x = rangeCheck (value);
            	}
       	 }
       	//Setting property for y coordinates of the screen 
       	 public int Y
        	{
        	           	//get Property
get
            	{
               		 return y;
            	}
		//set Property
            	set
            	{
                		y = rangedCheck (value);
            	}
        	}
       	//RangeChecked method for x coordinate
       	 private static int rangeCheckX(int x)
        	{
           		 if(x < 0 || x > 600)
            	{
               		 throw new ArgumentOutOfRangeException(“X”);
            	}
        	}
       	 //RangeChecked method for y coordinate
       	 private static int rangeCheckY(int y)
        	{
           		 if(y< 0 || y > 800)
            	{
                		throw new ArgumentOutOfRangeException(“Y”);
            	}
        	}
    }
get Accessors
If you use a property in a read only context, the compiler automatically translates your field-like code into a call to the get accessor of that property:
int x = topLeft.X;
set Accessors
Similarly, if you use a property in a write context, the compiler automatically translates your field-like code into a call to the set accessor of that property:
topLeft.X = 40;
Read/Write properties
When we want to use property in a read/write context, both get and set accessors are used:
int temp = topLeft.X.get{return X;}
topLeft.X.set{x = rangeCheckX(temp + 10);}
Read-only properties
Declare a property that contains only a get accessor or you want to use read-only property::
    struct ScreenPos
    {
         private int x, y;     
         public int X
         {
             get { return x; }
         }
     }
Write-only properties
Declare a property that contains only a set accessor or you want tot use write property::
   struct ScreenPos
   {
    	private int x, y;     
    	public int X
    	{
      		set { x = rangeCheckX(value); }
     	}
     } 
To Download Properties example Click here.
To view downloaded examples, Please Ensure that .NET Framework is installed on your system.
Scroll to Top