C # What is the difference between attributes and member variables?

Source: Internet
Author: User

Sometimes it is unclear whether a member variable or attribute is used for a class.
For example:
Member variables
Public string Name;
Or use attributes
Private string name
Public string Name
{
Get
{
Return name;
}
Set
{
Name = value;
}
}

Similar to member variables, attributes provide data storage, but attributes are far more powerful than member variables. Properties are accessed by special methods (Get and Set accessors. Get and Set accessors allow property value verification, other code execution, or other tasks after setting or retrieving Properties

For example

Write member variables like this
Public readonly string Name;
It can still be read-only.

Private string name
Public string Name
{
Get
{
Return name;
}

}

Object-Oriented programming methods are abstract and encapsulated. in classes, Defined variables are called domains for classes themselves. It can be public, private, and so on. The attribute is a feature of the class visible externally and is a feature that the class shows to external users. As we mentioned above, the domain can be public, but declaring the domain as public is not conducive to class encapsulation, because external users can directly modify the class. So we can use attributes. We only disclose their attributes. As for how to assign values (set) to them, how to perform values (get) has been encapsulated, the class is invisible to the outside. For external users, they can only use it, but cannot control it. How to Control Operations is determined by the class itself. Do you understand?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.