What are accessors of properties in C#?

Properties are an extension of fields and are accessed using the same syntax. They use accessors through which the values of the private fields can be read, written or manipulated.

The accessor of a property contains the executable statements that help in getting (reading or computing) or setting (writing) the property. Properties provide a controlled way to access class fields while maintaining encapsulation.

Syntax

Following is the syntax for property accessors in C# −

public datatype PropertyName {
   get {
      return fieldName;
   }
   set {
      fieldName = value;
   }
}

The get accessor returns the property value, while the set accessor assigns a new value using the implicit value parameter.

Types of Property Accessors

Accessor Type Purpose Usage
get Retrieves the property value Read-only or read-write properties
set Sets the property value Write-only or read-write properties
Both get and set Full read-write access Most common property type

Using Get and Set Accessors

Example

using System;

class Student {
   private string code;
   private int age;

   public string Code {
      get {
         return code;
      }
      set {
         code = value;
      }
   }

   public int Age {
      get {
         return age;
      }
      set {
         if (value >= 0 && value <= 120) {
            age = value;
         } else {
            Console.WriteLine("Invalid age");
         }
      }
   }
}

class Program {
   public static void Main() {
      Student student = new Student();
      student.Code = "CS101";
      student.Age = 20;
      
      Console.WriteLine("Code: " + student.Code);
      Console.WriteLine("Age: " + student.Age);
      
      student.Age = -5; // Invalid age
   }
}

The output of the above code is −

Code: CS101
Age: 20
Invalid age

Auto-Implemented Properties

C# provides auto-implemented properties for simple get/set operations without additional logic −

Example

using System;

class Person {
   public string Name { get; set; }
   public int Age { get; set; }
   public string Email { get; private set; } // Read-only from outside

   public Person(string name, int age, string email) {
      Name = name;
      Age = age;
      Email = email; // Can set internally
   }
}

class Program {
   public static void Main() {
      Person person = new Person("Alice", 25, "alice@example.com");
      
      Console.WriteLine("Name: " + person.Name);
      Console.WriteLine("Age: " + person.Age);
      Console.WriteLine("Email: " + person.Email);
      
      person.Name = "Alice Smith"; // Can modify
      person.Age = 26;
      // person.Email = "new@example.com"; // Compile error - private set
      
      Console.WriteLine("Updated Name: " + person.Name);
   }
}

The output of the above code is −

Name: Alice
Age: 25
Email: alice@example.com
Updated Name: Alice Smith

Read-Only and Write-Only Properties

Example

using System;

class BankAccount {
   private double balance;
   private string password;

   public double Balance {
      get { return balance; } // Read-only property
   }

   public string Password {
      set { password = value; } // Write-only property
   }

   public void Deposit(double amount) {
      if (amount > 0) {
         balance += amount;
      }
   }

   public bool VerifyPassword(string inputPassword) {
      return password == inputPassword;
   }
}

class Program {
   public static void Main() {
      BankAccount account = new BankAccount();
      account.Password = "secret123";
      account.Deposit(1000);
      
      Console.WriteLine("Balance: $" + account.Balance);
      Console.WriteLine("Password valid: " + account.VerifyPassword("secret123"));
      Console.WriteLine("Password valid: " + account.VerifyPassword("wrong"));
   }
}

The output of the above code is −

Balance: $1000
Password valid: True
Password valid: False

Conclusion

Property accessors in C# provide controlled access to class fields through get and set methods. They enable encapsulation by allowing validation, computation, or other logic during field access, while maintaining simple syntax for property usage.

Updated on: 2026-03-17T07:04:35+05:30

960 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements