Open In App

Properties in C#

Last Updated : 09 Sep, 2025
Comments
Improve
Suggest changes
33 Likes
Like
Report

A property in C# is a member of a class that provides a flexible way to read, write or compute the value of a private field. It acts like a combination of a variable and a method.

  • Declared inside a class using { get; set; }.
  • get: returns the value of the field.
  • set: assigns a value to the field.
  • Can be read only (get only) or write only (set only).
  • Improves encapsulation by controlling access to fields.

Example: Property with backing field

C#
private int age;
public int Age
{
get { return age; }
set
{
 if (value >= 0) age = value;
 else Console.WriteLine("Age cannot be negative!");
 }
}

There are two main reasons to use properties:

  • To access private data members of the class from another class through accessors.
  • To protect members of the class so another class may not misuse those members.

Accessors

The block of “set” and “get” is known as Accessors. It is very essential to restrict the accessibility of the property. There are two types of accessors: get accessors and set accessors.

Example: Problem Without Properties

C#
using System;

public class C1{
    
    // Public data members (No restrictions)
    public int rn;
    public string name;

    // Private field (Cannot be accessed directly)
    private int marks = 35;
}

public class Geeks
{
    public static void Main(string[] args)
    {
        // Creating an object of class C1
        C1 obj = new C1();

        // Setting values to public data members
        obj.rn = 10000;
        obj.name = null;

        // Directly modifying private field is not possible
         obj.marks = 0; // Error

        Console.WriteLine("Name: {0} \nRoll No: {1}", obj.name, obj.rn);
    }
}

Output:

./Solution.cs(25,14): error CS0122: `C1.marks' is inaccessible due to its protection level
Compilation failed: 1 error(s), 0 warnings

Explanation:

  • The public fields can be modified directly, without restrictions.
  • The private fields cannot be accessed directly, making it difficult to modify values when needed.
  • We use properties for controlled access.

Syntax of Properties

This is the syntax for defining Properties:

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

These are the different terms used to define the properties

  • Access-modifiers: Used to define the accessibility levels. It can be public, private, protected or internal.
  • Return-type: Used to define which value returns with the end of the method.
  • Get accessor: Used to get the values.
  • Set accessor: Used to set or modify the values.

Get Accessor (Read-only Property)

It specifies that the value of a field can be accessed publicly. It returns a single value and it specifies the read-only property.

Example:

C#
using System;
class Geeks {
    // Private field
    private int r = 357;

    // Read-only property
    public int Roll_no
    {
        get { return r; }
    }
    public static void Main(String[] args)
    {
        Geeks o = new Geeks();
        Console.WriteLine("Roll no: " + o.Roll_no);
    }
}

Output
Roll no: 357

Set Accessor (Read-Write Property)

It will specify the assignment of a value to a private field in a property. It returns a single value and it specifies the write-only property.

Example:

C#
using System;

public class Geeks {
    private int r;

    // Read-Write Property
    public int RollNo
    {
        get { return r; }
        set { r = value; }
    }

    public Geeks(int rollNo) { this.RollNo = r; }
}

class MainClass {
    public static void Main(string[] args)
    {
        Geeks o = new Geeks(147);
        Console.WriteLine($ "Current Roll No: {o.RollNo}");

        // Modify RollNo using setter
        o.RollNo = 357;
        Console.WriteLine($ "Changed Roll No: {o.RollNo}");
    }
}

Output
Current Roll No: 0
Changed Roll No: 357

Types of Properties

  1. Read and Write Properties: When the property contains both get and set methods.
  2. Read-Only Properties: When the property contains only the get method.
  3. Write Only Properties: When the property contains only a set method.
  4. Auto-Implemented Properties: When there is no additional logic in the property accessors it is introduced in C# 3.0.

Accessor Accessibility

  • We can not use accessor modifiers on an interface or an explicit interface member implementation.
  • We can use accessor modifiers only if the property has both set and get accessors.
  • If the property is an override modifier, the accessor modifier must match the accessor of the overridden accessor.
  • The accessibility level on the accessor must be more restrictive than the accessibility level on the property.

Example 1: Read-Write property example

C#
using System;

public class Student {
    private int rollNo;

    // Read-Write Property
    public int RollNo
    {
        get { return rollNo; }
        set { rollNo = value; }
    }
    public Student(int rollNo) { this.RollNo = rollNo; }
}

public class Geeks {
    public static void Main(string[] args)
    {
        Student obj = new Student(147);
        Console.WriteLine($ "Roll No: {obj.RollNo}");

        obj.RollNo = 123;
        Console.WriteLine($
                          "Changed Roll No: {obj.RollNo}");
    }
}

Output
Roll No: 147
Changed Roll No: 123

Example 2: Auto-implemented property example

C#
using System;

public class Student{
    // Auto-implemented property
    public string Name { get; set; } = "GFG";
}

class Geeks{
    public static void Main(string[] args){
        Student s = new Student();

        Console.WriteLine("Name: " + s.Name);
        
        // Changing property value
        s.Name = "Geeky";
        Console.WriteLine("Changed Name: " + s.Name);
    }
}

Output
Name: GFG
Changed Name: Geeky
Suggested Quiz
5 Questions

When can a read-only property be assigned a value in C#?

  • A

    At declaration or inside a constructor

  • B

    Anywhere in the class

  • C

    Only inside methods

  • D

    Only inside static methods

Explanation:

A read-only property is like a readonly field and can be set only once at initialization or in constructors.

Which of the following is true about properties in C#?

  • A

    They replace constructors

  • B

    They provide controlled access to private fields

  • C

    They are only used with static classes

  • D

    They cannot have access modifiers

Explanation:

Properties act as intermediaries between private fields and external code, controlling read/write access.

What are the two accessors in C# properties?

  • A

    get and put

  • B

    get and set

  • C

    read and write

  • D

    fetch and assign

Explanation:

get retrieves a value, and set assigns a value to a property.

Which type of property was introduced in C# 3.0 to simplify code?

  • A

    Indexer

  • B

    Auto-implemented property

  • C

    Virtual property

  • D

    Sealed property

Explanation:

Auto-implemented properties allow simpler syntax without explicitly writing backing fields.

What is the purpose of access modifiers in property accessors?

  • A

    To improve performance

  • B

    To make properties faster than methods

  • C

    To control accessibility of get and set separately

  • D

    To replace constructors

Explanation:

Access modifiers allow different accessibility levels for get and set (e.g. public get, private set).

Image
Quiz Completed Successfully
Your Score :   2/5
Accuracy :  0%
Login to View Explanation
1/5 1/5 < Previous Next >

Explore