Codectionary / Developer documentation / C#

Properties (get/set)

Properties are C#'s idiomatic way to expose class data through accessor-like syntax while still allowing controlled access via get and set. Unlike a plain public field, a property can validate a value before it's set, compute a value on the fly, or restrict access to read-only. Auto-implemented properties (using { get; set; } with no body) provide a concise shorthand when no custom logic is needed.

Syntax

public Type PropertyName { get; set; }

Examples

Auto-Implemented Properties

The concise shorthand for simple properties with no custom logic.

public class Person
{
    public string Name { get; set; } = "";
    public int Age { get; set; }
}

Person p = new Person();
p.Name = "Fola";
p.Age = 21;

Console.WriteLine($"{p.Name} is {p.Age}");

Properties with Validation Logic

A full property with a backing field, letting the setter validate incoming values.

public class Product
{
    private double _price;

    public double Price
    {
        get => _price;
        set
        {
            if (value < 0)
                throw new ArgumentException("Price cannot be negative");
            _price = value;
        }
    }
}

Product item = new Product();
item.Price = 25;
Console.WriteLine(item.Price);  // 25

try
{
    item.Price = -10;  // throws
}
catch (ArgumentException e)
{
    Console.WriteLine(e.Message);
}

Read-Only and Computed Properties

A property with only a getter is effectively read-only from outside the class.

public class Circle
{
    public double Radius { get; }  // set only in the constructor

    public Circle(double radius)
    {
        Radius = radius;
    }

    public double Area => Math.PI * Radius * Radius;  // computed, expression-bodied
}

Circle c = new Circle(5);
Console.WriteLine(c.Area);  // 78.5398...
// c.Radius = 10;  // Error - no setter available

init-only Properties

init lets a property be set during object creation (including with object initializers) but not modified afterward.

public class Order
{
    public string Id { get; init; } = "";
    public double Total { get; init; }
}

Order order = new Order { Id = "ORD-1", Total = 49.99 };
Console.WriteLine($"{order.Id}: {order.Total}");

// order.Total = 100;  // Error - init-only, can't change after creation

Best practices

  • Use auto-implemented properties ({ get; set; }) by default, and switch to a full property with a backing field only when you need validation or computed logic
  • Use { get; } (or init) instead of { get; set; } for values that should never change after an object is created
  • Prefer properties over public fields for anything exposed outside the class - it keeps the door open to add logic later without breaking existing callers
  • Use expression-bodied properties (=> expression) for simple computed values to keep the class concise

At a glance

Purpose
Applications on the .NET platform
File extension
.cs
Runs in
.NET runtime
Usually used with
.NET SDK and libraries

Specifications & further reading

Related C# documentation

Classes & Objects
A class is a blueprint for creating objects, bundling related data (fields/properties) and behavior (methods) together. C# is a fully object-oriented language - every application has at least one class, typically with a Main method as its entry point. An object is a specific instance of a class, created with the new keyword, with its own independent copy of the class's instance data.
Constructors
A constructor is a special method that runs automatically when an object is created with new, used to initialize the object's state. A constructor shares its name with the class and has no return type. C# supports constructor overloading (multiple constructors with different parameter lists) and constructor chaining with this(...), where one constructor calls another in the same class to avoid duplicating initialization logic.
Access Modifiers
Access modifiers control the visibility of classes, fields, methods, and properties from other parts of a program. C# provides public (accessible from anywhere), private (accessible only within the declaring class - the default for class members), protected (accessible within the class and its subclasses), internal (accessible only within the same assembly/project), and combinations like protected internal and private protected.
Inheritance & base
Inheritance lets a class (the derived or child class) reuse and extend the members of another class (the base or parent class), written as class Child : Parent. The base keyword gives access to the parent class's members from within the child, most commonly used to call the parent's constructor so the child doesn't have to duplicate its setup logic. Unlike some languages, C# only supports single class inheritance - a class can extend just one base class, but can implement multiple interfaces.