C # Study Notes 3,

Source: Internet
Author: User

C # Study Notes 3,

1.Although not necessary, we generally recommend that you put each class in its own file and name the file with the class name, you can easily find the code that defines a specific class.

 

2.If you do not add an access token for a class member, the primary account uses private, that is, the member is a private member by default.

 

3.No matter which naming mode is used for private fields (_ FirstName, _ firstName, m_firstName (continuation of sub-C ++ naming rules, m indicates member variable), firstName ), all public fields and attributes must be named according to Pascal's case sensitivity specification. Therefore, public attributes should use names in the FirstName format. Similarly, if you do not create an attribute that encapsulates a public field, you should use Pascal case sensitivity for this field.

 

4.In the implementation of an attribute in C #2.0, you can specify an access modifier for get or set (but not for both ), overwrite the access modifier specified for the attribute declaration. When you specify an access modifier for get or set, note that the access modifier must be more restrictive than the access modifier applied to the entire attribute. For example, if you declare an attribute as a strict private object, but declare its set as a loose public object, a compilation error will occur.

 

5.The object initializer is only a syntactic improvement. The ClL code is generated and the field or attribute value is assigned with a separate statement. in c # language, the sequence of the member initiators is the same, determines the order in which attributes and fields are assigned values in the statements called by the constructor of The ClL code. Similarly, the set initiator will generate add () in IL to add elements.

 

6.Terminator: C # provides the finalizer structure, which is different from the destructor of C ++, the Terminator does not run immediately after all references to an object disappear. On the contrary, the terminator runs after the last activity of an object and before the program ends. Specifically, the garbage collector identifies the objects with the terminator during the next garbage collection process. After that, it does not recycle these objects immediately, but adds them to an end queue, an independent thread traverses every object in the same end team, calls their Terminator, and removes them from the queue so that they can be used by the garbage collector again.

 

7.Constructor chain: in the same class {implement against the same object), call another constructor from one constructor, C # The syntax format is to add this keyword after a colon, and then add a list of parameters corresponding to the CAPTCHA caller. To avoid repeated code input, constructor chaining is implemented by the constructor initialization, the constructor will determine which constructor to call before executing the implementation of the current constructor.

 

8.Anonymous types: they are data types dynamically generated by the compiler, rather than declared by explicit class definitions. Use new {property =, property =, value} (if the assigned value is an attribute or field, if the name is not explicitly specified, the name of a field or attribute is recognized by the compiler. When the compiler encounters an anonymous syntax, it will automatically generate a pencil class, this class has attributes that correspond to the named values and Data Types in the anonymous type declaration. Although C # does not provide a name swing for this automatically generated type, it is still static. Unless Lambda expressions and Query expressions are used to associate data of different types or perform horizontal projection of data, the anonymous type should be avoided in general.

 

9.A special example of a static class is that the C # compiler will automatically mark it as abstract and sealed in the pencil code, which will designate the class as non-extensible.

 

10.Static constructor: the static constructor is used to initialize classes (rather than class instances). The static constructor is not explicitly called, on the contrary, the "RunTime" will automatically call the static constructor when accessing the class for the first time. The so-called "first runtime class" may be to call a common instance constructor, it may also be a static method or attribute of the Question class. We use the static constructor to initialize static data in the class into a specific value, especially when initialization values cannot be obtained through a simple value assignment during declaration. If the assignment of a static field is performed both in the static constructor and during declaration, what value will be obtained at the end of initialization?

The answer is that the value assignment during static initialization takes precedence over the value assignment during field declaration, because the value assignment during static declaration moves to the first sentence of the static constructor during the IL code generated by the C # compiler. The same is true for instance fields.

 

11.Constant const: the constant Field automatically becomes a static field, because you do not need to generate a new field instance for each object instance. However, if you explicitly declare a constant field as static, this may cause a compilation error.

 

12.Nested class: A class is not used externally or has special significance. It only makes sense to the current class and can be declared as a nested class. Nested classes are modified using private. This member in a nested class refers to an instance of the nested class, rather than an inclusive class. to access an inclusive instance of the nested class, one way is to display the instances that pass the inclusive class, for example, you can use a constructor or this method parameter. Another interesting feature of the nested class is that it can access any member of the inclusive class, including private members. Otherwise, the inclusion class cannot access private members of the nested class. Be cautious about public Nested classes, which mean bad encoding styles.

 

13.Partial classification and some methods: partial classification (partial calss) is mainly used for generating fixed-format code by code production tools, the partial classification statement can put multiple partial classifications in multiple files (must be in the same assembly), and the compiler will automatically merge them.

C #3.0 adds the partial methods feature. Some methods can only exist in the partial classification without returning values. They are similar to the partial classification and mainly facilitate code generation. Some methods allow the declaration of a method without an implementation. However, if an optional implementation is included, this implementation can be placed in the classification definition of a sister department. In the generated partial classification code, the generation tool cannot provide specific method implementation logic. If a hook (placeholder) is provided in the form of some methods, the specific implementation can be implemented by developers, if some methods are not implemented in another classification, no trace of some methods will appear in IL. For example, the details of some methods and partial categories are displayed in the Person class.

// Person. designer. cs file public partial class Person {# region partial void OnLastNameChanging (string value); partial void OnFirstNameChanging (string value); # endregion public Guid Id {get; set ;} private string _ LastName; public string LastName {get {return _ LastName;} set {if (_ LastName! = Value) {OnLastNameChanging (value); _ LastName = value ;}} private string _ FirstName; public string FirstName {get {return _ FirstName;} set {if (_ FirstName! = Value) {OnFirstNameChanging (value); _ FirstName = value ;}}// Person cs file public partial class Person {partial void OnLastNameChanging (string value) {if (string. isNullOrWhiteSpace (value) {throw new ArgumentException ("LastName") ;}} partial void OnFirstNameChanging (string value) {if (string. isNullOrWhiteSpace (value) {throw new ArgumentException ("FirstName ");}}}

 

------------------------- The above content is organized according to "C # the third edition of the essence theory ".

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.