csharp / beginner
Snippet
Fluent Object Property Assignment
Object initializers allow you to assign values to any accessible fields or properties of an object at creation time without invoking a specific constructor.
snippet.cs
csharp
1
2
3
4
5
6
7
8
9
public class Player {public string Name { get; set; }public int Level { get; set; }}var hero = new Player {Name = "Aris",Level = 10};
Breakdown
1
new Player { ... }
Initiates the object and starts the property assignment block.
2
Name = "Aris"
Directly sets the Name property immediately after instantiation.