VOOZH about

URL: https://www.geeksforgeeks.org/c-sharp/properties-in-c-sharp/

⇱ Properties in C# - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Properties in C#

Last Updated : 23 Apr, 2026

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

There are two main reasons to use properties:

  • To provide controlled and secure access to private data members, including validation and logic 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. They can be used to restrict or control accessibility based on design requirements. There are two types of accessors: get accessors and set accessors.

Example: Problem Without Properties

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:


Output
Roll no: 357

Set Accessor (Write-Only 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:


Output
Current Roll No: 147
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


Output
Roll No: 147
Changed Roll No: 123

Example 2: Auto-implemented property example


Output
Name: GFG
Changed Name: Geeky
Comment

Explore