Coding/Learn/csharp/properties and constructors
C# Foundational Course
01.Why C# & .NET Power Modern Software
02.Anatomy of a C# Application
03.Variables & The C# Type System
04.Console I/O: Reading & Writing
05.String Interpolation & Modern Formatting
06.Arithmetic, Math Class & Modulo
07.Conditional Branching: if, else if, and else
08.Logical Operators (&&, ||, !)
09.Switch Statements & Modern Switch Expressions
10.While & Do-While Loops
11.The Standard For Loop & Counting
12.Loop Control: break and continue
13.Methods: Modular, Reusable Code Blocks
14.Optional Parameters, Named Arguments & out/ref
15.Fixed-Size Arrays & Zero-Based Indexing
16.The Sacred Foreach Loop
17.Dynamic Collections: List<T> & Generics
18.Classes & Objects: The OOP Blueprint
19.Auto-Properties & Constructors
20.Capstone: RPG Character & Inventory Engine
Course Certificate0%

Complete all lessons to earn

LearnAuto-Properties & Constructors
19 / 20 0%
Lesson 19 of 20
5 mins read

Auto-Properties & Constructors

PreviousNext Lesson
In Plain English

Public fields leave data vulnerable to invalid states (like setting health = -9999). C# properties provide safe getters and setters with the clean auto-property syntax ({ get; set; }). Constructors ensure objects are initialized with valid required data the moment they are created.

The Mental Model

"A raw public field is like leaving the front door of a bank vault wide open for anyone to grab cash. A property with { get; set; } is like a trained security guard standing at the vault door checking ID and validating transactions."

Deep Dive: How It Works

Auto-Properties: public string Title { get; set; } (the compiler generates a hidden private backing field automatically).

Read-Only Properties: public int Id { get; } or { get; init; } (can only be set during construction).

Constructors: Special methods with the EXACT same name as the class and no return type. Runs automatically when new is called: public User(string name) { Name = name; }.

Validation inside Setters: You can add validation logic inside set { if (value >= 0) _health = value; }.

Syntax Blueprint

class User {
  public string Name { get; set; }
  public int Age { get; set; }

  public User(string name, int age) {
    Name = name;
    Age = age;
  }
}

Auto-properties provide encapsulated data access; constructor ensures initialization.

PointCore Rules to Remember

Point star
The { get; set; } Standard: The industry-standard way to expose object data in C# and .NET.
Point star
Constructor Initialization: Prevents half-baked objects with uninitialized null fields.
Point star
init Keyword: Allows setting property during new initialization while keeping it immutable afterward.

Common Beginner Traps & How to Fix Them

Mistake:Giving a constructor a void return type: public void Player()

Why it happens: Treating constructors like normal methods.

How to fix: Constructors NEVER have a return type—not even void.

Pro Tip: In C# 12, you can use primary constructors directly on the class header: class User(string name, int age) { }!

Live Interactive Example

Hit Run Code to see it live
Properties & Constructor Showcase
C# 12 / .NET
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
Output Console
Click "Run Code" to view the rendered output.
How it works: The constructor initializes Hostname and Region, and Connect updates state.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Create a Product with constructor

  • PointDefine class Product with public string Name { get; set; } and public decimal Price { get; set; }.
  • PointAdd a constructor public Product(string name, decimal price) { Name = name; Price = price; }
  • PointIn Main(), instantiate new Product("Laptop", 999m) and print "Product: Laptop | Price: $999".
1
2
3
4
5
6
7
8
9
10
11
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates