Open In App

C# | Properties

Before going on properties lets have a look at why the concept of properties came into C#? The is because of two reasons:
 

Example:
 






// C# program to illustrate the problems
// with public and private members
using System;
 
// public class
public class C1
{
     
    // public data members
    public int rn;
    public string name;
     
    // private field
    // private int marks = 35;
 
}
 
// another public class
public class C2
{
     
// Main Method
public static void Main(string[] args)
{
     
    // Creating object of C1 class
    C1 obj = new C1();
     
    // setting values to public
    // data members of class C1
    obj.rn = 10000;
    obj.name = null;
     
    // setting values to private
    // data members of class C1
    // obj.mark = 0;
     
    // display result
    Console.WriteLine("Name:  {0} \nRoll No: {1}", obj.name, obj.rn);
     
}
}

Output: 
 

Name:   
Roll No: 10000

Explanation: In above you can see that public members of class C1 can be accessed by class C2 and using the object “obj” of C1 it can provide the values to the members like Name is given value null but we don’t want this to be null. C2 cannot provide the value to the member “marks” because it is private in C1. To test the private member access remove the comments and try to run and you can see the compiler will give an error. The Programming languages which do not have properties, use getter and setter methods to provide such access mechanism.
 



Using Properties

Properties are the special type of class members that provides a flexible mechanism to read, write, or compute the value of a private field. Properties can be used as if they are public data members, but they are actually special methods called accessors. This enables data to be accessed easily and helps to promote the flexibility and safety of methods. Encapsulation and hiding of information can also be achieved using properties. It uses pre-defined methods which are “get” and “set” methods which help to access and modify the properties.
Accessors: The block of “set” and “get” is known as “Accessors”. It is very essential to restrict the accessibility of property. There are two type of accessors i.e. get accessors and set accessors. There are different types of properties based on the “get” and “set” accessors:
 

The syntax for Defining Properties:
 

<access_modifier> <return_type> <property_name>
{
        get { // body }
        set { // body }
}

Where, <access_modifier> can be public, private, protected or internal. <return_type> can be any valid C# type. <property_name> can be user-defined. Properties can be different access modifiers like public, private, protected, internal. Access modifiers define how users of the class can access the property. The get and set accessors for the same property may have different access modifiers. A property may be declared as a static property by using the static keyword or may be marked as a virtual property by using the virtual keyword. 
 

class Geeks {
// Declare roll_no field
private int roll_no;

// Declare roll_no property
public int Roll_no 
{ 

   get 
     {
      return roll_no;
     }

}
}
class Geeks {

// Declare roll_no field
private int roll_no;

// Declare roll_no property
public int Roll_no
 { 

   get 
     {
         return roll_no;
      }

   set 
     {
         roll_no = value;
      }
}
}

Accessor Accessibility 
 

Below are the programs to demonstrate different types of properties:
Program 1: To demonstrate the Read-Only property using “get” accessor.
 




// C# program to illustrate the
// read-only property
using System;
 
public class Student {
 
    // Declare counter field as cnt
    private static int cnt;
  
    // to define constructor
    public Student()
    {
 
        // increment the counter
        // using constructor
        cnt++;
    }
  
    // Declare counter property
    public static int Counter
    {
 
        // read-only property
        get
        {
            return cnt;
        }
    }
}
 
class StudentTest {
 
    // Main Method
    public static void Main(string[] args)
    {
 
        // create three instances of
        // Student class it call constructor
        // three times which increase the counter
        Student s1 = new Student();
        Student s2 = new Student();
        Student s3 = new Student();
  
        // s1.Counter = 10;
        // Compile Time Error: Can't set value of
        // Counter because it is read only.
  
        Console.WriteLine("Total No of Student: " + Student.Counter);
  
        // Program Give Warning
        // The variable `s1' is assigned but its value is never used
    }
}

Output: 
 

Total No of Student: 3

Program 2: To demonstrate the both read & write property using “get” and “set” accessors.
 




// C# program to illustrate the
// read and write property
using System;
 
public class Student {
 
    // Declare name field
    private string name = "GeeksforGeeks";
  
    // Declare name property
    public string Name
    {
 
       get
        {
            return name;
        }
 
        set
        {
            name = value;
        }
    }
}
 
class TestStudent {
 
    // Main Method
    public static void Main(string[] args)
    {
        Student s = new Student();
  
        // calls set accessor of the property Name,
        // and pass "GFG" as value of the
        // standard field 'value'.
        s.Name = "GFG";
  
        // displays GFG, Calls the get accessor
        // of the property Name.
        Console.WriteLine("Name: " + s.Name);
    }
}

Output: 
 

Name: GFG

 


Article Tags :
C#