Read the summary and summary of learning hard c # LEARNING notes. 3,

Source: Internet
Author: User

Read the summary and summary of learning hard c # LEARNING notes. 3,

I have been busy with my work recently. I have several projects in my hand waiting for my independent development and design. So I usually don't have much time on work days. I don't want to change my work when I get tired of work, so I have a little time on weekends, today, I spent an afternoon summing up and sorting out the key points in the book. During the sorting process, I found some shortcomings in the book. I have explained them in the following blog posts, if you have this book, you can compare it with the combination of knowledge points and books. If you do not have this book, you can also perform actual coding testing and learning based on the knowledge points I have compiled, I hope it will help you. If you think it is okay, please recommend it. Thank you!

Reading the summary and summary series of learning hard c # LEARNING notes from this blog post, the knowledge points involved will become more and more in-depth. I hope you will benefit from this article. If you find any shortcomings, please also point out, thank you!

First, add a previous knowledge point:

The delegate can only be defined in the namespace and class, but not in the method (I .e., it cannot be partially defined ),This is not explained in the book. I realized it when I was writing code. Please test it to see if you can find out the root cause.

13.Generic Variability

Covariant: a generic type parameter can be implicitly converted from a derived class to the base class. The out keyword is used to identify the type parameter to indicate that it supports covariant. (That is, you can assign a child type parameter generic object to the type parameter generic object of the parent type), such:

List <object> objectList = new List <object> ();

List <string> stringList = new List <string> ();

ObjectList. AddRange (stringList );

 

Invert: a generic type parameter can be implicitly converted from a base class to a derived class. The in keyword is used to identify the type parameter to indicate that it supports inversion. (That is, you can assign a value to a child type parameter generic object of the parent type parameter ).

The opposite is true for the inverter and the covariant. It can be understood that the covariant is the process of replacing the subclass of the generic type parameter with the parent class.

 

Note:

 

14.Null type

Int? NullInt = null; nullInt = nullInt ?? 0; the final value of nullInt is 0;

 

 

15th.Anonymous Method

Delegate type anonymousMethod = delegate (parameter list)

{

// Method subject

};

 

 

16.Iterator

1. The principle of foreach loop is to access the IEumerator and continuously obtain the next object (MoveNext, Current) until the next object cannot be found. If a foreach loop is required for an object, the object type must be implemented by the IEumerable or IEumerable <T> interface, and the GetEnumerator method must be implemented to return to the iterator.

2. The implementation of the GetEnumerator method may use the yield return keyword to dynamically generate the iterator (generation of the iterator is done by the compiler)

 

17. C #3.0New Features

Class Attribute simplified definition (automatically implement attributes ):Access ModifierTypeProperty name {get; set ;}You do not need to declare private variables to store attribute values. The Compiler automatically generates these values during compilation.

 

Implicit type: Use the var keyword to replace the type of the defined variable (similar to the Javascript var). The actual type of the variable is equal to the type of its value, because the implicit type needs to be determined based on the value of the variable, variable Initialization is required when defining the implicit type variable, if an uninitialized value or its initialized value cannot directly obtain the actual type (for example, NULL), an error is returned.

Note that the implicit type (var) can only be declared in local variables. It cannot be declared as the member type (field, attribute, method) of the class and the type of the parameter of the method.

 

Implicit array: when instantiating an array, you do not need to specify the array type and number. directly assign values to each member of the array (using the set initiator). However, you must note that the types of array members must be consistent, otherwise, an error is returned because the actual type cannot be obtained and the definition of the implicit type is not met. The syntax is as follows:

Var intArray = new [] {1, 2, 4, 5, 6, 7, 8, 9, 10 };

 

Object initializer: when a new constructor is called (any public constructor that can be used as an instance is not limited to a constructor without parameters ), assign values to members of different types. The definition syntax is as follows:

TypeVariable name = newType (Parameter ){Property =Value ,... ...};

Example: Person p = new Person () {Name = "zuowenjun", Age = 29}; Person p = new Person ("zuowenjun") {Age = 29, Sex ="Male "};

 

Set initiator:When a new Type constructor is called (any public constructor that can be used as an instance is not limited to a constructor without parameters), members of each type are assigned directly, note that the Add method must be implemented. The Compiler automatically calls the Add method to Add a set.

The syntax is as follows:

TypeVariable name = newType (Parameter ){Member object ,... ...};

Example:

List <Person> personList = new List <Person> () {new Person {Name ="Zhang San ", Age = 10}, new Person {Name ="Li Si ", Age = 20 }};

String [] strs = new [] {"a", "B", "c", "d "};

Dictionary <int, string> dic = new Dictionary <int, string> (4) {1, "a" },{ 2, "B" },{ 3, "c" },{ 4, "d "}};

 

Anonymous type: you do not need to define a type. You can instantiate an unknown type object by using the implicit type and object initiator. The definition syntax is as follows:

VarVariable name = new {Property name =Value };

Note: The default access range of the anonymous type is Internal and cannot be changed. Attribute members can only be assigned values, and other Members except attributes (events, indexers, etc.) cannot be defined ), the default access modifier for Attribute members cannot be added. The attribute type is the actual type of the value, and the attribute type is not limited.

Example:

Func <string, string> sayFunc = delegate (string yourname) {return yourname +"Hello. Nice to meet you! ";};

Var Person = new {Name = "zuowenjun", Age = 29, Sex ="Male ", WebSite =" www.zuowenjun.cn ", Say = sayFunc };

Console. WriteLine (Person. Say ("Blog Garden "));

Output: blog,Hello. Nice to meet you!

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.