Sometimes, for a class, I can't figure out whether to use member variables or attributes.
like:
Member variables
public string Name;
Or use attributes
private string name
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
Attributes are similar to member variables. They both provide data storage, but the functions of attributes are much more powerful than member variables. Properties are accessed by special methods (Get and Set Accessories). Get and Set Accessories allow you to verify property values, execute other codes, or perform other tasks after setting or retrieving properties.
For example
This is what the member variable is written
public readonly string Name;
It can be read-only
private string name
public string Name
{
get
{
return name;
}
}
Object-oriented programming methods are to abstract and encapsulate; in a class, the variables defined are directly for the class itself, and we call them domains. It can be public , private, etc.; attributes are attributes for the class seen outside and are attributes displayed to external users. As we mentioned earlier, the domain can be public, but declaring the domain as public in this way will be detrimental to the encapsulation of the class, because external users can directly modify the class. So we can use attributes. We just expose their attributes. As for how to assign values (sets) and get values (gets) they have been encapsulated and are invisible to the outside of the class. For external users, they can only use it, not control it, and how to control operations is determined by the class itself. Do you understand?