I've been looking into C# Properties, and I'm a little confused over what you gain by some suggested examples, such as Microsoft's.
Their class property example is as follows:
public class SaleItem
{
string name;
decimal cost;
public SaleItem(string name, decimal cost)
{
this.name = name;
this.cost = cost;
}
public string Name
{
get => name;
set => name = value;
}
public decimal Price
{
get => cost;
set => cost = value;
}
}
What does their example give you, over declaring your class properties such as:
public class SaleItem
{
public string Name { get; set; };
public decimal Cost { get; set; };
public SaleItem(string name, decimal cost)
{
Name = name;
Cost = cost;
}
}
I think the usage of this
and =>
is throwing me off, as I'm not too familiar with them, but I generally dont understand what the difference is between these two examples, or why you might choose one over the other.
Apologies if this is too general a topic, but anyone can clarify any maybe point me in the direction of some useful resources, it would be appreciated.