C # differences between assets and field variables

Source: Internet
Author: User

Note: I am more accustomed to calling property an asset rather than an attribute. If you know the meaning of propery, you may understand my point of view.

Just writtenProgramWhen I always think that a field can meet the requirements, why should I use attributes that are difficult to define?
To get and set the de-definition,CodeThe Difference and advantages are unknown.
Example:

Public class myclass
{

Public static string

Private sting _ B = "";
Public String getval
{
Get {retrun _ B ;}
Set {_ B = value ;}
}
}

Myclass. A and myclass. getval may implement the same function. Why must we define attributes?

the advantages of attributes compared with common variables are summarized below:
1. Data Security
because a is a public variable, it can be arbitrarily changed outside the class. To prevent any changes caused by users, attribute get and set can be used to restrict _ B's read-only, write-only or control _ B's maximum or minimum
reference a man's short answer: "In fact, custom variables have implemented attributes by default, but they are only simple to read and write. We need to add more restrictions to the defined attributes based on reading and writing. after you learn more, you will find that many things are added for data security. to put it bluntly, it is a limitation . For example, you must specify the method in an interface (note that it is mandatory and mandatory) to implement "
2. One principle of OOP and programming is Data encapsulation, which means that the field type should not be provided to the outside world in a public manner.
3, In fact, it is not difficult to simply write a program. It is rare to design a program to make the code concise and easy to read, with the highest efficiency. How to ensure the safe operation of the program.

Related Article

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.