top

Search

C# Tutorial

Properties in C# provide a flexible mechanism to handle private fields. They are named members of classes, structures etc. The member variables or methods are called fields. Properties use accessors to read, write or manipulate the values of private fields.Some of the salient points on properties are given as follows:Properties allow a public way of getting and setting values in a class. The implementation and verification code is hidden.The get and set accessors are used in properties. The get accessor returns the property value while a set accessor sets a new value.Some properties have both get and set accessor(read-write), some have only a get accessor (read only) while some have only a set accessor (write only).The value that is assigned by the set assessor is defined using the value keyword.Use of Assessors in PropertiesThe get and set accessors are used in properties. The get accessor(reading) returns the property value while a set accessor(writing) sets a new value.An example that demonstrates the use of get and set accessor is given as follows:Source Code: Program to implement get and set accessor in C#public int Data {    get    {       return this.data;    }    set    {       this.data = value;    } }Program using get and set AccessorsA program that demonstrates properties using both get and set accessors is given as follows:using System;  namespace PropertiesDemo {   class Example   {     char data;          public char Data     {         get         {             return this.data;         }                  set         {             this.data = value;         }     }   }     class Example   {      static void Main()     {         Example obj = new Example();         obj.Data = 'A';                   Console.WriteLine("Implementing Properties in C#");          Console.WriteLine("The data value is: {0}", obj.Data);      }   } }The output of the above program is as follows:Implementing Properties in C# The data value is: ANow let us understand the above program.The class Example contains a char data type data. The get accessor returns the value of data while the set accessor sets the data to value. The code snippet for this is given as follows:  class Example   {     char data;          public char Data     {         get         {             return this.data;         }                  set         {             this.data = value;         }     }   }In the function Main(), the object obj of class Example is defined. The value of data is stored as ‘A’. Then this value is printed. The code snippet for this is given as follows:static void Main()    {        Example obj = new Example();        obj.Data = 'A';        Console.WriteLine("Implementing Properties in C#");        Console.WriteLine("The data value is: {0}", obj.Data);    }
logo

C# Tutorial

Properties in C#

Properties in C# provide a flexible mechanism to handle private fields. They are named members of classes, structures etc. The member variables or methods are called fields. Properties use accessors to read, write or manipulate the values of private fields.

Some of the salient points on properties are given as follows:

  1. Properties allow a public way of getting and setting values in a class. The implementation and verification code is hidden.
  2. The get and set accessors are used in properties. The get accessor returns the property value while a set accessor sets a new value.
  3. Some properties have both get and set accessor(read-write), some have only a get accessor (read only) while some have only a set accessor (write only).
  4. The value that is assigned by the set assessor is defined using the value keyword.

Use of Assessors in Properties

The get and set accessors are used in properties. The get accessor(reading) returns the property value while a set accessor(writing) sets a new value.

An example that demonstrates the use of get and set accessor is given as follows:

Source Code: Program to implement get and set accessor in C#

public int Data
{
   get
   {
      return this.data;
   }
   set
   {
      this.data = value;
   }
}

Program using get and set Accessors

A program that demonstrates properties using both get and set accessors is given as follows:

using System; 
namespace PropertiesDemo
{
  class Example
  {
    char data;
    
    public char Data
    {
        get
        {
            return this.data;
        }
        
        set
        {
            this.data = value;
        }
    }
  }
 
  class Example
  { 
    static void Main()
    {
        Example obj = new Example();
        obj.Data = 'A'; 
        
        Console.WriteLine("Implementing Properties in C#"); 
        Console.WriteLine("The data value is: {0}", obj.Data); 
    }
  }
}

The output of the above program is as follows:

Implementing Properties in C#
The data value is: A

Now let us understand the above program.

The class Example contains a char data type data. The get accessor returns the value of data while the set accessor sets the data to value. The code snippet for this is given as follows:

  class Example
  {
    char data;
    
    public char Data
    {
        get
        {
            return this.data;
        }
        
        set
        {
            this.data = value;
        }
    }
  }

In the function Main(), the object obj of class Example is defined. The value of data is stored as ‘A’. Then this value is printed. The code snippet for this is given as follows:

static void Main()
   {
       Example obj = new Example();
       obj.Data = 'A';
       Console.WriteLine("Implementing Properties in C#");
       Console.WriteLine("The data value is: {0}", obj.Data);
   }

Leave a Reply

Your email address will not be published. Required fields are marked *

Suggested Tutorials

Swift Tutorial

Introduction to Swift Tutorial
Swift Tutorial

Introduction to Swift Tutorial

Read More

R Programming Tutorial

R Programming

Python Tutorial

Python Tutorial