Read the summary and summary of the knowledge points of learning hard C # Learning notes

Source: Internet
Author: User

Recently work more busy, there are several projects on hand to my independent development and design, so usually working days without too much time, work tired do not want to move, also on the weekend a little time, today I spent an afternoon to continue to summarize and finishing the main points, in the process of finishing, found some of the book's shortcomings, I have explained in the following blog post, if you have this book can be compared with the knowledge point and book combination of the way to see, no this book can also be compared with my knowledge of the key points of the actual coding test and learning, hope to be helpful to everyone, if you feel that you can, please recommend Oh, thank you!

Read the learning Hard C # Learning notes Summary and summary series of articles from this blog, the knowledge points involved will be more and more in-depth, I hope everyone can benefit, if found to have shortcomings, please also point out, thank you!

First, add a previous point of knowledge:

A delegate can be defined only in a namespace, in a class, and not in a method (that is, it cannot be defined locally). This is not stated in the book, I was in writing code when I realized, but also please test, see if you can find the root cause.

13. Trans-generic variability

Covariant: Refers to a generic type parameter that can be implicitly converted from a derived class to a base class, identifying the type parameter with the Out keyword to indicate that it supports covariance. (That is, you can assign a type parameter generic object of a subtype to a type parameter generic object of the parent type), such as:

List<object> objectlist=new list<object> ();

List<string> stringlist=new list<string> ();

Objectlist.addrange (stringlist);

contravariant: A generic type parameter can be implicitly converted from a base class to a derived class, using the In keyword to identify the type parameter to indicate that it supports contravariance. (That is, you can assign a type parameter generic object of the parent type to the type parameter generic object of the subtype).

Contravariance is the reverse of covariance, which can be understood as the process of replacing subclasses of a generic type parameter with a parent class, which is the process of replacing subclasses with the parent class of a generic type parameter.

Attention:

    1. Covariance and contravariance are supported only by interfaces and delegates
    2. Covariance and Contravariance only support reference types, because there is a reference conversion to the variability, and the value type does not have
    3. You must explicitly use the in or out keyword to identify the type parameter, otherwise it is not supported by default
    4. Delegate variability is not used in multicast delegates

14. Nullable Types

    1. nullable< value type, value type? are represented as nullable types, which are value types that are allowed to be null values.
    2. Empty merge operator: Double question mark (i.e.:?? ), the function is if the nullable type (where the nullable type refers to a nullable type, the value that contains the reference type) is not NULL then returns its value otherwise the specified value is returned, such as:

Int? Nullint=null; Nullint= nullint?? 0; The final value of the nullint is 0;

    1. A nullable type if the value is null, when it is converted to a reference type, the boxing operation does not occur, because the reference type itself supports a null value, and a boxing operation occurs instead of a null value. If a boxed nullable type is converted to a nullable type, a unboxing operation occurs, and if the value is null the nullable type value after unpacking is also null;
    2. Using the nullable type's GetType method, the actual value type is returned, and if the TypeOf method is used, the nullable type is returned, and if the actual value of the nullable type is null, then both methods are called to error.

15. Anonymous Methods

    1. The definition of an anonymous method is basically the same as a normal method definition, but the anonymous method has no method name, return type, uses the delegate keyword to define the method, and must assign the anonymous method to the appropriate delegate type (that is, by using an anonymous method to instantiate the delegate), and then execute it using the delegate. The definition syntax is as follows:

Delegate type anonymousmethod=delegate (formal parameter list)

{

Method body

};

    1. The life cycle of an anonymous method is the same as the life cycle of the delegate object that references the anonymous method;
    2. An external variable referenced by an anonymous method is the same as the life cycle of an anonymous method;

16. iterators

The 1.foreach loop principle is to stop the loop by accessing the iterator (Ieumerator) and then constantly getting the next object (MoveNext, current) until the next object is not found. If an object needs to use a Foreach loop, the object's type must implement the Ieumerable or Ieumerable<t> interface, and the GetEnumerator method must be implemented to return the iterator.

2. Implementing the GetEnumerator method may dynamically generate an iterator with the yield return keyword (the build of the iterator is done by the compiler)

17. c#3.0 new features

The properties of the class are simplified (auto-implemented properties): access modifier type property name {get;set;}, the compiler does not need to declare a private variable to hold the value of the property, which is generated automatically at compile time.

Implicit type: Use the var keyword instead of the type that defines the variable (like the JavaScript var), where the actual type of the variable equals the type of its value, because the implicit type needs to come out of its true type based on the value of the variable, so defining an implicitly typed variable must be initialized at the same time. Uninitialized or initialized values that cannot be obtained directly from the actual type (for example, NULL) will cause an error.

Note implicit types (VAR) can only be declared in local variables and cannot be declared as member types of a Class (fields, properties, methods), and parameter types of methods

Implicit arrays: When instantiating an array, you do not need to specify the type and number of arrays, directly assign the array members (using the set initializer), but note that the type of the array members must be uniform, otherwise the actual type cannot be obtained, and the implicit type of the definition requirements, will be error. The definition syntax is as follows:

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

Object initializers: When you call a constructor of a type by new (any public constructor that can be instantiated, not a constructor that is not qualified as an argument), assign the members of each type directly, and define the syntax as follows:

type variable name =new type ( parameter) { attribute = value,... ...};

Example: Person p=new person () {name= "Zuowenjun", age=29}; Person P=new person ("Zuowenjun") {age=29,sex= " Male"};

Collection Initializers: When you call a constructor of a type by new (any public constructor that can be instantiated, not a constructor that is not qualified as an argument), the members of each type are directly assigned, but it is important to note that the Add method must be implemented, The compiler implements the addition of the collection by automatically calling the Add method.

The definition syntax is as follows:

type variable name =new type ( parameter) { member object,... ...};

Example:

list< person> personlist=new list< person> () {new person{name= " Zhang San", age=10}, new person{ Name= " John Doe", 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 types: That is, there is no need to define a type, an object of an unknown type is instantiated by an implicit type and an object initializer, and the syntax is defined as follows:

Var variable name =new{ Property name = value};

Note: The access scope of an anonymous type is internal by default and cannot be changed, only attribute member assignments can be made, other members (events, indexers, etc.) other than attributes cannot be defined, and property members cannot add access adornments defaults think public, the type of the property is the actual type of its value, and the type of the property value is not.

Example:

func<string,string> sayfunc=delegate (string yourname) {return yourname + "hello. Nice to meet you! ";};

var person = new {Name = ' zuowenjun ', age = $, Sex = ' male ', WebSite = ' www.zuowenjun.cn ', Say = sayfunc};

Console.WriteLine (Person.say (" Blog Park"));

Output: Blog Park, , hello. Nice to meet you!

Read the summary and summary of the knowledge points of learning hard C # Learning notes

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.