Types Of Properties In C#

In this blog, we will learn about Types of Properties in C#.

There are 3 types of properties in C#, they are as follows.

Read Only Properties are properties without a set accessor and are considered read-only.

private readonly int roomNo = 1;
public int RoomNo {
    get {
        return roomNo;
    }
}

Write Only Properties are properties without a get accessor and are considered write-only.

private int roomNo
public int RoomNo {
    set {
        roomNo = value;
    }
}

 Read Write Properties are properties with both a get and set accessor and are considered as read-write properties.

private int roomNo;
public int RoomNo {
    get {
        return roomNo;
    }
    set {
        roomNo = value;
    }
}

Summary

C# Properties are used to compute the data before allowing it to modify, Properties can take an action when data is modified like raising an event or changing the value of other fields.

Next Recommended Reading Data Types & Type Conversions in C#