Item1: Always use properties instead of accessible data members
Attributes are an important feature in C #. We can use attributes to encapsulate members.
From the compiler's point of view, attributes are two methods for member variables: get_xxx and set_xxx. This is very similar to Java. When we define a member variable in Java, then, when the corresponding attributes are generated through refactoring, the corresponding method names are get_xxx and set_xxx.
Why should attributes be used to replace Member variables? I think fromClass responsibilitiesIt is better to understand this. When a class exposes an access entry, this entry can be a public method, attribute, or member variable.Encapsulation changesFrom the point of view, the class itself should handle exceptions of its own variables, rather than the caller. Therefore, if you insist on using open member variables, it is unreasonable to handle variable exceptions only by the caller. On the one hand, the same exception handling is performed by Multiple callers, resulting in a large number of duplicates.CodeOn the other hand, when the business changes and the logic of the member variable changes, you need to modify the place where the variable is called. This will be a very crazy thing.
Using attributes to replace Member variables has the following benefits:
- You can perform unified processing on exception conditions in set or get, or perform unified processing on multiple threads in set and get.
- You can set the attribute to read/write by setting set and get, or you can set the attribute to read-only by setting only get and not setting set, this prevents the caller from incorrectly assigning values to properties.
- Since set and get can be seen as methods, they can use method access delimiters. For example, you can set to private and set get to public, for an attribute, its caller still cannot assign values to it, and the read-only attribute is implemented from another perspective. Of course, you can also use other delimiters, such as protected and internal.
- You can declare attributes in the interface or abstract attributes in the abstract class. In this way, sufficient space can be provided for specific businesses during framework design.
- We can expand the extension of the attribute to discuss the index. The form of the index is this [int I] {set; get}. The index also includes set and get, it can also be a number or another string in. When we bind data through the indexer, it must be a number in.
Attribute usage will decreaseProgram?
Generally, this is not true. First ,. the net program runs in JIT Mode. if it affects performance, it can only be run for the first time. Secondly, when the compiler compiles attributes, inline functions are used to process set and get. The result is very different from that of directly accessing data members, which is negligible.
Note: When we refactor the member variables exposed in the class into attributes, we need to re-compile all the classes that use this attribute, because this refactoring operation breaks the binary compatibility.