6 ms·
I really love c# - one of my favrorite languages, but can any one explain the benefit of this new syntax? Person p1 = new(); Person p2 = new("Tom"); Pers
by syspec 6y ago
I really love c# - one of my favrorite languages, but can any one explain the benefit of this new syntax?
Person p1 = new();
Person p2 = new("Tom");
Person p3 = new() { FirstName = "Tom" };
- goto11 6y agoYou save typing by not having to repeat the class name.
- twodave 6y agoIn my opinion it's more useful when declaring class members, e.g.: public class SignPost { public List<string> Messages { get; set;} = new(); private List<string> Errors = new(); } In both of the above usages var is not allowed. Often-times instead of a simple list you have something like an IReadOnlyCollection<T> or an equally-abominable custom type that is just a pain to write out 2x for every time you add it as a dependency somewhere.