Skip to content

CSharp Properties Initialization

Joe Care edited this page Dec 22, 2025 · 1 revision

C# Properties and Object Initialization

This lesson introduces properties and basic initialization patterns in C#.

Related wiki pages:

  • Properties intro: Lessons.md (to be renamed to a properties-focused file)
  • General overview: CSharpBible.md

Official docs:


1. Auto-properties and constructor initialization

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

    public Person()
    {
        Age = 30;
        Name = "Alice";
        Hometown = "Springfield";
    }
}

// Usage
var person = new Person();
Console.WriteLine(person.Name);

2. Properties with backing fields

class UserAccount
{
    private string _name;
    private string _address;

    public UserAccount(string name, string address)
    {
        _name = name;
        _address = address;
    }

    public string Name
    {
        get => _name;
        set => _name = value;
    }

    public string Address
    {
        get => _address;
        set => _address = value;
    }
}

// Usage
var account = new UserAccount("Bob", "123 Main St");
Console.WriteLine(account.Name);

3. Object initializer syntax

var person = new Person
{
    Age = 25,
    Name = "Carol",
    Hometown = "Berlin"
};

Object initializers are often used in examples throughout this wiki, including MVVM view models in CSharpBible-MVVM-Basics.md.


4. Next steps

  • Read the more detailed properties lesson in Lessons.md (to be renamed) and related MVVM topics.
  • Explore init-only properties and immutable types.

Clone this wiki locally