-
Notifications
You must be signed in to change notification settings - Fork 2
CSharp Properties Initialization
Joe Care edited this page Dec 22, 2025
·
1 revision
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:
- Properties: https://learn.microsoft.com/dotnet/csharp/programming-guide/classes-and-structs/properties
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);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);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.
- Read the more detailed properties lesson in
Lessons.md(to be renamed) and related MVVM topics. - Explore
init-only properties and immutable types.