.NET
getters and setters
programming
C#
object-oriented programming

Getter and Setter declaration in .NET

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

In .NET, getters and setters are usually written as C# properties instead of explicit GetX() and SetX() methods. Properties let you expose state with a natural syntax while still giving you room to add validation, access restrictions, or computed behavior when needed.

Auto-Properties

The simplest declaration is an auto-property:

csharp
1public class User
2{
3    public string Name { get; set; } = string.Empty;
4    public int Age { get; set; }
5}

The compiler creates the hidden backing field for you. This is the standard choice when the property is just storing and returning a value with no extra logic.

Full Properties With a Backing Field

When you need validation or custom behavior, declare an explicit field and write the accessor bodies yourself.

csharp
1public class Product
2{
3    private decimal _price;
4
5    public decimal Price
6    {
7        get
8        {
9            return _price;
10        }
11        set
12        {
13            if (value < 0)
14            {
15                throw new ArgumentOutOfRangeException(nameof(value));
16            }
17
18            _price = value;
19        }
20    }
21}

This pattern is useful when a property is part of a business rule rather than just a storage slot.

Read-Only and Restricted Setters

Not every property should be publicly writable. C# lets you omit the setter or reduce its visibility.

csharp
1public class Order
2{
3    public Guid Id { get; } = Guid.NewGuid();
4}
csharp
1public class Account
2{
3    public decimal Balance { get; private set; }
4
5    public void Deposit(decimal amount)
6    {
7        if (amount <= 0)
8        {
9            throw new ArgumentOutOfRangeException(nameof(amount));
10        }
11
12        Balance += amount;
13    }
14}

This keeps outside code from changing important values directly.

init for Construction-Time Assignment

Modern C# also supports init, which allows assignment during object creation but not ordinary later mutation.

csharp
1public class Customer
2{
3    public string Name { get; init; } = string.Empty;
4    public string Email { get; init; } = string.Empty;
5}
6
7var customer = new Customer
8{
9    Name = "Ada",
10    Email = "[email protected]"
11};

This is useful for immutable-style data objects where values should be set once and then stay stable.

Computed Properties

A property does not have to store data. It can calculate a value from other properties.

csharp
1public class Rectangle
2{
3    public double Width { get; set; }
4    public double Height { get; set; }
5
6    public double Area => Width * Height;
7}

Area has only a getter because it is derived, not assigned independently.

When to Use Methods Instead

Properties work best when getting or setting a value is cheap and conceptually simple. If the operation:

  • performs heavy work
  • starts network or disk activity
  • changes broad object state

then a method is often clearer than a property because callers do not expect complex side effects from ordinary property access.

Common Pitfalls

The biggest pitfall is writing manual backing fields for every property even when an auto-property would do. That adds boilerplate without improving the API.

Another common mistake is exposing public setters for values that should only change through business methods. If a property must obey rules, let those rules live in controlled methods or validated setters.

Developers also sometimes hide expensive or surprising logic in getters. A property read should usually feel lightweight and unsurprising to the caller.

Summary

  • In .NET, getters and setters are normally declared as C# properties.
  • Use auto-properties when no custom logic is needed.
  • Use backing fields when you need validation or custom setter behavior.
  • Restrict writes with private set, omit the setter, or use init for construction-only assignment.
  • Use methods instead of properties when the action is not simple value access.

Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.