C# tip (Primary Constructor)

View this class;

public class Product
{
   public string Name {get; set;}
   public decimal Price {get; set;}

   public Product(string name, decimal price)
   {
       Name = name;
       Price = price;
   }
}

This can be re-written as;

public class Product(string name, decimal price)
{
   public string Name {get; set;} = name;
   public decimal Price {get; set;} = price;
}

Seems we can save some lines with this new pattern.

FavoriteLoadingAdd to favorites
Spread the love

Author: Shahzad Khan

Software developer / Architect