English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C# Properties (Property)

Properties are the named members of classes, structures, and interfaces. Member variables or methods in a class or structure are called Fields. Properties are an extension of fields and can be accessed using the same syntax. They use Accessors to make the values of private fields readable and writable or manipulable.

Properties do not determine the storage location. Instead, they have readable and writable or calculated values Accessors.

For example, there is a class named Student with private fields of age, name, and code. We cannot directly access these fields outside the class scope, but we can have properties that access these private fields.

Accessors

Property ofAccessorsContains executable statements that help to get (read or calculate) or set (write) properties. Accessor declarations can include a get accessor, a set accessor, or both. For example:

// Declare a Code property of type string
public string Code
{
   get
   {
      return code;
   }
   set
   {
      code = value;
   }
}
// Declare a Name property of type string
public string Name
{
   get
   {
     return name;
   }
   set
   {
     name = value;
   }
}
// Declare an Age property of type int
public int Age
{ 
   get
   {
      return age;
   }
   set
   {
      age = value;
   }
}

Online Example

The following example demonstrates the usage of properties (Property):

using System;
namespace w3codebox
{
   class Student
   {
      private string code = "N.A";
      private string name = "not known";
      private int age = 0;
      // Declare a Code property of type string
      public string Code
      {
         get
         {
            return code;
         }
         set
         {
            code = value;
         }
      }
   
      // Declare a Name property of type string
      public string Name
      {
         get
         {
            return name;
         }
         set
         {
            name = value;
         }
      }
      // Declare an Age property of type int
      public int Age
      {
         get
         {
            return age;
         }
         set
         {
            age = value;
         }
      }
      public override string ToString()
      {
         return "Code = " + Code +", Name = " + Name + ", Age = " + Age;
      }
    }
    class ExampleDemo
    {
      public static void Main()
      {
         // Create a new Student object
         Student s = new Student();
            
         // Set student's code, name, and age
         s.Code = "001";
         s.Name = "Zara";
         s.Age = 9;
         Console.WriteLine("Student Info: {0}", s);
         // Increase Age
         s.Age += 1;
         Console.WriteLine("Student Info: {0}", s);
         Console.ReadKey();
       }
   }
}

When the above code is compiled and executed, it will produce the following result:

Student Info: Code = 001, Name = Zara, Age = 9
Student Info: Code = 001, Name = Zara, Age = 10

Abstract Properties

Abstract classes can have abstract properties, which should be implemented in derived classes. The following program illustrates this point:

using System;
namespace w3codebox
{
   public abstract class Person
   {
      public abstract string Name
      {
         get;
         set;
      }
      public abstract int Age
      {
         get;
         set;
      }
   }
   class Student : Person
   {
      private string code = "N.A";
      private string name = "N.A";
      private int age = 0;
      // Declare a Code property of type string
      public string Code
      {
         get
         {
            return code;
         }
         set
         {
            code = value;
         }
      }
   
      // Declare a Name property of type string
      public override string Name
      {
         get
         {
            return name;
         }
         set
         {
            name = value;
         }
      }
      // Declare an Age property of type int
      public override int Age
      {
         get
         {
            return age;
         }
         set
         {
            age = value;
         }
      }
      public override string ToString()
      {
         return "Code = " + Code +", Name = " + Name + ", Age = " + Age;
      }
   }
   class ExampleDemo
   {
      public static void Main()
      {
         // Create a new Student object
         Student s = new Student();
            
         // Set student's code, name, and age
         s.Code = "001";
         s.Name = "Zara";
         s.Age = 9;
         Console.WriteLine("Student Info:",- {0}
         // Increase Age
         s.Age += 1;
         Console.WriteLine("Student Info:",- {0}
         Console.ReadKey();
       }
   }
}

When the above code is compiled and executed, it will produce the following result:

Student Info: Code = 001, Name = Zara, Age = 9
Student Info: Code = 001, Name = Zara, Age = 10