Auto-Implemented Properties
Declare properties with concise auto syntax.
Auto-Implemented Properties is a free C# Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Is a Property?
A property looks like a field from the outside but is backed by accessor methods (get and set). Properties let you expose data while keeping control over how it is read and written.
using System;
class Program
{
public string Name { get; set; }
static void Main()
{
var p = new Program();
p.Name = "Coddy";
Console.WriteLine(p.Name);
}
}Auto-Implemented Syntax
An auto-implemented property uses { get; set; }. The compiler generates a hidden backing field for you, so you do not write one by hand.
using System;
class Person
{
public string FirstName { get; set; }
public int Age { get; set; }
}
class Program
{
static void Main()
{
var person = new Person { FirstName = "Ada", Age = 36 };
Console.WriteLine(person.FirstName + " is " + person.Age);
}
}Why Use Properties Over Fields?
Public properties give you a stable API. You can later add validation or change the storage without breaking callers. Public fields cannot do that.
using System;
class Account
{
public decimal Balance { get; set; }
}
class Program
{
static void Main()
{
var a = new Account();
a.Balance = 100.50m;
Console.WriteLine("Balance: " + a.Balance);
}
}Read-Only Auto-Properties
Omit set to make a property read-only. It can only be assigned in the constructor or via an initializer, giving you immutable-by-default values.
using System;
class Circle
{
public double Radius { get; }
public Circle(double radius)
{
Radius = radius;
}
}
class Program
{
static void Main()
{
var c = new Circle(5.0);
Console.WriteLine("Radius: " + c.Radius);
}
}Default Property Values
You can give an auto-property an initial value with = value;. This runs when the object is created, before any constructor body.
using System;
class Settings
{
public bool DarkMode { get; set; } = true;
public int Volume { get; set; } = 50;
}
class Program
{
static void Main()
{
var s = new Settings();
Console.WriteLine("DarkMode: " + s.DarkMode + ", Volume: " + s.Volume);
}
}Private Setters
A private set lets the class change the value internally while preventing outside code from modifying it. This is a common encapsulation pattern.
using System;
class Counter
{
public int Count { get; private set; }
public void Increment()
{
Count++;
}
}
class Program
{
static void Main()
{
var counter = new Counter();
counter.Increment();
counter.Increment();
Console.WriteLine("Count: " + counter.Count);
}
}Init-Only Setters
The init accessor (C# 9+) allows assignment only during object initialization. After construction the value is locked.
using System;
class Product
{
public string Name { get; init; }
public decimal Price { get; init; }
}
class Program
{
static void Main()
{
var p = new Product { Name = "Book", Price = 12.99m };
Console.WriteLine(p.Name + ": " + p.Price);
}
}Properties in Different Access Levels
Properties follow normal access modifiers. You can mix levels: a public getter with a private setter, or an entirely internal property.
using System;
class User
{
public string Id { get; private set; }
public User(string id)
{
Id = id;
}
}
class Program
{
static void Main()
{
var u = new User("u-001");
Console.WriteLine("User Id: " + u.Id);
}
}Computed-Looking Auto-Properties
Auto-properties store data; they do not compute. If you need a calculated value, you use a full property (covered later). For now, treat auto-properties as simple storage.
using System;
class Rectangle
{
public double Width { get; set; }
public double Height { get; set; }
}
class Program
{
static void Main()
{
var r = new Rectangle { Width = 4, Height = 3 };
double area = r.Width * r.Height;
Console.WriteLine("Area: " + area);
}
}Updating Auto-Properties
You can read and reassign a settable auto-property as many times as you like, just like a variable.
using System;
class Player
{
public int Score { get; set; }
}
class Program
{
static void Main()
{
var player = new Player();
player.Score = 10;
player.Score += 5;
player.Score *= 2;
Console.WriteLine("Score: " + player.Score);
}
}Putting It Together
A typical model class is mostly auto-properties: some settable, some init-only, some with private setters. This keeps the class concise and clear.
using System;
class BankAccount
{
public string Owner { get; init; }
public decimal Balance { get; private set; }
public void Deposit(decimal amount)
{
Balance += amount;
}
}
class Program
{
static void Main()
{
var acct = new BankAccount { Owner = "Lin" };
acct.Deposit(250m);
Console.WriteLine(acct.Owner + ": " + acct.Balance);
}
}Quick Check
Test your understanding of auto-implemented properties.
Recap
You learned that auto-implemented properties use { get; set; } with a compiler-generated backing field. Variations include read-only { get; }, private set for controlled mutation, and init for set-once values. They are the default way to expose data on a class.
using System;
class Demo
{
public string A { get; set; } = "settable";
public string B { get; } = "read-only";
public string C { get; init; } = "init-only";
}
class Program
{
static void Main()
{
var d = new Demo { C = "changed-at-init" };
Console.WriteLine(d.A + ", " + d.B + ", " + d.C);
}
}Frequently asked questions
Is the “Auto-Implemented Properties” lesson free?
Yes — the full text of “Auto-Implemented Properties” is free to read here on the web, and the C# Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.
What will I learn in “Auto-Implemented Properties”?
Declare properties with concise auto syntax. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C# Academy?
No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Auto-Implemented Properties” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C# Academy lesson?
Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Auto-Implemented Properties
- Full Properties with Backing Fields
- Expression-Bodied Members
- Indexers