I'm currently programming in C# and found this snippet in one of the tutorials.
What exactly do the curly braces in this method mean? Is it like a key value pair {id: 2}
?
weapon = new Weapon(new WeaponData() { Id = 12 });
I'm currently programming in C# and found this snippet in one of the tutorials.
What exactly do the curly braces in this method mean? Is it like a key value pair {id: 2}
?
weapon = new Weapon(new WeaponData() { Id = 12 });
This is what's called an obect initializer. It allows you to set the values of properties right after the obect is constructed. It's equivalent to the following code:
var weaponData = new WeaponData();
weaponData.Id = 12;
weapon = new Weapon(weaponData);
In this case, the weapon
class has a no args constructor which is being called, in the same line, its initialising the id
property with a value of 12. Its just another way of initialising the object