I really like these, particularly when combined with some of the new syntactic sugar
Console.WriteLine("Hello, CS12_Playing : PrimaryConstructor");
Product p
= new(1024,
"Thingie");Console.WriteLine(p);
Product p2
= new(1025,
"Thingie.v2",
"New super Thingie");Console.WriteLine(p2);
Product p6
= new(1026,
null);Console.WriteLine(p6);
Product p7
= new(1027,
"",
"weird prototype");Console.WriteLine(p7);
public class Product(int id, string? name, string description = "no description")
{
public int Id { get { return id; } }
public string Name
{
get;
set;
} = (name
is null || name
== string.Empty) ? "no Name provided" : name
;
public string Description { get; set; } = description;
public override string ToString()
{
return $"Product: {this.Id}, {this.Name}, {this.Description} ";
}
}
No Constructor required when parameters added to newed class instance.