Automatic attributes are used in c to reduce code input
- 2020-05-07 20:20:07
- OfStack
public class Product
{
private String name;
public String Name
{
get
{
return name;
}
private set
{
name = value;
}
}
private Decimal price;
public Decimal Price
{
get
{
return price;
}
set
{
price = value;
}
}
public Product(String name, Decimal price)
{
this.price = price;
this.name = name;
}
}
It can be rewritten as:
public class Product
{
public String Name
{
get;
private set;
}
public Decimal Price
{
get;
set;
}
public Product(String name, Decimal price)
{
Name = name;
Price = price;
}
public override string ToString()
{
return String.Format("{0}:{1}", this.Name, this.Price);
}
}
Doesn't the code simplify a lot!
Note:
Properties that cannot be defined as read-only or write-only must be provided at the same time
If you want to add judgment, validation, and other logic to attributes, you can only use the traditional method of attribute definition