Types of Properties in C# Programming

Properties

In C#, properties are a member that provides a flexible mechanism to read, write, or compute the value of a private field.

C# is one of the first languages that offers direct support of Properties. Properties looks similar to a public variable on which we can do get() or set() operations.

Syntax

The following is the syntax of Properties:

  1. //Define the Properties Accessors  
  2. Public < type > < Property Name >   
  3. {  
  4.     Get  
  5.     {  
  6.         Return  
  7.         <var > ;  
  8.     }  
  9.     Set   
  10.     {  
  11.         If(Is Valid(Value))  
  12.         <var > = Value;  
  13.     }  
  14. }  
In the syntax shown above:
  1. The access modifier can be Private, Public, Protected or Internal.

  2. The return type can be any valid C# data type, such as a string or integer.

  3. The ”this” keyword shows that the object is for the current class.

  4. The get() and set() operations of the syntax are known as accessors.

There are the following 4 types of Properties:

  1. Read-Write Property

  2. Read-Only Property

  3. Static Property

  4. Indexer Property

Read and write Property

Programmers allow you to access the internal data of a class in a less cumbersome manner. Earlier programmers were required to define two separate methods, one for assigning a value of a variable and the other for retrieving the value of a variable. When you create a property, the compiler automatically generates class methods to set() and get() the property value and makes calls to these methods automatically when a programmer uses the property .

Here is a simple program of a Read and Write Property.



Figure 1: Read and write property

Here is the code for for a read-write property.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. namespace Read_and_Write_Property   
  6. {  
  7.     class student   
  8.     {  
  9.         public string Myname = "";  
  10.         public int Myage = 0;  
  11.   
  12.         //Declare a Name Property of type String  
  13.         public string Name   
  14.         {  
  15.             get  
  16.             {  
  17.                 return Myname;  
  18.             }  
  19.             set  
  20.             {  
  21.                 Myname = value;  
  22.             }  
  23.         }  
  24.   
  25.         //Declare an Age Property of type int  
  26.   
  27.         public int Age  
  28.         {  
  29.             get   
  30.             {  
  31.                 return Myage;  
  32.             }  
  33.             set   
  34.             {  
  35.                 Myage = value;  
  36.             }  
  37.         }  
  38.   
  39.         public override string ToString() {  
  40.             return ("Name=" + Name + ",Age= " + Age);  
  41.         }  
  42.   
  43.     }  
  44.   
  45.     class Program   
  46.     {  
  47.         static void Main(string[] args)  
  48.         {  
  49.             Console.WriteLine("This is Read and Write Propety");  
  50.   
  51.             // Create a new object for student class   
  52.             student s = new student();  
  53.             Console.WriteLine("Student details:" + s);  
  54.             s.Name = "Nilesh";  
  55.             s.Age = 24;  
  56.             Console.WriteLine("Student details:" + s);  
  57.             //increment the age property  
  58.             s.Age += 1;  
  59.             Console.Write("Student details:" + s);  
  60.             Console.ReadKey();  
  61.   
  62.         }  
  63.     }  
  64. }  
Read-Write Property Output



Figure 2: Output for read and write property

Read-Only Property

A read-only Property has a get accessor but does not have any set() operation. This means that you can retrieve the value of a variable using the read-only property but you cannot assign a value to the variable.

Here is the Code
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. namespace ConsoleApplication1   
  6. {  
  7.     public class PropertyHolder   
  8.     {  
  9.         private int Myage = 0;  
  10.         public PropertyHolder(int PropVal)   
  11.         {  
  12.             Myage = PropVal;  
  13.         }  
  14.         public int age  
  15.         {  
  16.             get {  
  17.                 return Myage;  
  18.             }  
  19.         }  
  20.     }  
  21.   
  22.     class Program   
  23.     {  
  24.         static void Main(string[] args)   
  25.         {  
  26.             PropertyHolder p = new PropertyHolder(24);  
  27.             Console.WriteLine("My age is: " + p.age);  
  28.             Console.ReadKey();  
  29.         }  
  30.     }  
  31. }  
Read-Only Output



Figure 3: Read only property output

Static Property

A static Property can be used to access only the static members of the class.

Here is the code
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. namespace Static_Property   
  6. {  
  7.     public class CounterClass   
  8.     {  
  9.         private static int number = 0;  
  10.         public CounterClass()  
  11.         {  
  12.             number++;  
  13.         }  
  14.         public static int NumberofObjects  
  15.         {  
  16.             get {  
  17.                 return number;  
  18.             }  
  19.   
  20.             set {  
  21.                 number = value;  
  22.             }  
  23.         }  
  24.     }  
  25.   
  26.     class Program   
  27.     {  
  28.         static void Main(string[] args)  
  29.         {  
  30.             Console.WriteLine("Number of Objects: {0}", CounterClass.NumberofObjects);  
  31.   
  32.             CounterClass object1 = new CounterClass();  
  33.             Console.WriteLine("Number of Objects: {0}", CounterClass.NumberofObjects);  
  34.   
  35.             CounterClass object2 = new CounterClass();  
  36.             Console.WriteLine("Number of Objects: {0}", CounterClass.NumberofObjects);  
  37.   
  38.             CounterClass object3 = new CounterClass();  
  39.             Console.WriteLine("Number of Objects: {0}", CounterClass.NumberofObjects);  
  40.             Console.ReadKey();  
  41.   
  42.   
  43.         }  
  44.     }  
  45. }  
Static Property Output



Figure 4: Static property output

This is all about Properties. I hope you like it. In my next article I'll tell you about Indexers. Thank you for reading.