C# Properties

Properties in C# are known as the members of a class that can be used to set and get the data from a data field of a class. Properties are not particularly used to store data in C#, they are just like an interface to transfer the data. Properties are defined as the named members of the class. They are used as the public data members but they are special methods called Accessors.

Accessors:

In C# we have the special methods which can be used to set and get the values from the underlying data member, known as Accessors.

using System;
namespace tutorialspoint {
  public class Employee {
      private string Id = "unknown";
      private string name = "unknown";
      private int age = 0;
      
      // Declaring Code property of string type :
      public string ID {
         get {
            return Id;
         }
         set {
            Id = value;
         }
      }
      
      // Declaring Name property of string type :
      public string Name {
         get {
            return name;
         }
         set {
            name = value;
         }
      }
      
      // Declaring Age property of int type :
      public int Age {
         get {
            return age;
         }
         set {
            age = value;
         }
      }
      public override string ToString() {
         return "Id = " + Id +", Name = " + Name + ", Age = " + Age;
      }
   }
   
 public  class Example {
      public static void Main() {
      
         // Creating new Employee object:
         Employee e = new Employee();
         
         // Setting Id, name and the age of the Employee
         e.ID = "6";
			 e.Name = "JOHN";
         e.Age = 45;
         Console.WriteLine("1Employee Data: {0}", e);
         
		  e.ID = "10";
         e.Name = "ALICE";
         e.Age = 38;
         Console.WriteLine("2Employee Data: {0}", e);
         
		  
		  e.ID = "22";
         e.Name = "ESE";
         e.Age = 41;
         Console.WriteLine("3Employee Data: {0}", e);
       
      }
   }
}
Assessors have 2 types Set Accessor and Get Accessor.

Set Accessors:

Set accessors are used to set the fixed variable, named Value into a data field.

Get Accessors:

Get accessors are used to get the data from the data field. You can not set the data with this accessor.

Types of Properties:

Following are the types of properties.

  • Read Write property
  • Write only property
  • Read-only property
  • Auto-implemented property
Properties DescriptionSyntax
Read Write propertyUsed to read and write the data from the
data field into the data field. It contains 2 accessors i.e. set and get.
AccessModifier DataType PropertyName
{
set { DataFieldName = value; }
get { Return DataFieldName; }
}
Write only property Used to write the data into the data field of a
class. It can’t read the data from the data field. It contains only 1 accessor i.e. set accessor.
AccessModifier Datatype PropertyName {  set { DataFieldName = value; } }
Read-only propertyUsed to read the data from the data field.
Contains 1 Accessor i.e “get”
 AccessModifier Datatype PropertyName { get { return DataFieldName; } }
Auto-implemented property Used to reduce the amount of code that we have to write. It creates a private, the anonymous
field that is going to hold the data.
Access specifier Datatype Property name { get; set; }