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

Source: Internet
Author: User

Another weekend, just have time, and continue to review and summarize, hope to let everyone benefit, the shortcomings of the welcome to correct, thank you!

18. Lambda

1.Lambda expression: Another representation of an anonymous method, which can contain expressions and statements, and is used to create a delegate or convert to an expression tree, defining the syntax:( formal parameter list) =>{ method body}; Single argument, you can omit parentheses

The 2.LAMBDA expression tree is a data structure used to represent the lambda expression logic, which represents the code as an object tree, rather than an executable code, as defined by the syntax (A+B):

Experssion<func<int,int,int>> (A, b) =>a+b;

The lambda expression tree is dynamically constructed from code, as follows:

ParameterExpression A=expression.parameter (typeof (int), "a");p arameterexpression A=expression.parameter (typeof ( int), "B"); Binaryexpression Body=expression.add (A, b); Experssion<func<int,int,int>> Exprtree=expression.lambad (BODY,A,B);

3.Lambda expression tree Converts an expression tree to executable code (that is, the delegate represented) by calling the Compile method

19. extension Methods

1. An extension method is a method used to extend a method member of a known type (i.e., attaching a new method without altering the original type);

2. Extension method definition rules:

A) must be defined in a non-nested, non-generic static class (that is, a standard static Class);

b) It must have at least one parameter;

c) The first parameter must be in the type plus the This keyword, indicating that the type corresponding to the parameter is expanded;

d) The first parameter cannot use any modifiers (such as: cannot use rel,out)

e) The type of the first parameter cannot be a pointer type;

3. The extension method definition syntax is as follows:

Static class name {public         static return type extension method name (this target extension type parameter name, other parameters ...)         {                   //method body        }}    

4. Null reference (NULL) supports calling extension methods

Note: When defining an extension method, to prevent the method from polluting (that is, the extended method has no practical or unwanted effect in some types), you should extend the specific type as far as possible, rather than extending its base class

Twenty. Linq

1.LINQ, a language-integrated query, provides a unified query approach across a variety of data sources

A 2.LINQ query expression must begin with a FROM clause and must end with a select or a group clause, which can contain multiple child packages between the first FROM clause and the last Select or group clause, with the following definition syntax:

Var query=from N in numbers where n<=10 select N;

3.LINQ query expressions are based on lambda expressions and extension methods, and lambda expressions are based on delegates, and extension methods are methods, so the essence of LINQ is the invocation of methods, for the compiler, Using LINQ query expressions is exactly the same as code that uses method calls.

21. c#4.0 new features

Optional parameter : A parameter that specifies the default value, that is, when the method is called, the argument can specify an argument, or it can not specify an argument, or a default value if it is not specified;

Note: 1. Optional parameters must be followed by non-optional parameters, 2. The default value must be constant; 3. An indeterminate parameter (that is, an array of arguments represented by params) cannot be an optional parameter; 4. Parameters identified with Rel or out cannot be optional parameters;

named arguments: that is, when the method is called, the formal parameter name is specified directly and assigned a value (Specify the argument), not in the order of the method parameters, and the non-optional parameter must all specify an argument, using the following syntax:

static void Write (DateTime dt,string name,string result= "www.zuowenjun.cn")        {            Console.Write ("{0}-{1}-{2}", DT, name, result);        } Call method Write (name: "Dream On Journey", Dt:DateTime.Now);

 

Dynamic type: That is, using the dynamic keyword to represent the type of a variable, the dynamic type can not write the code explicitly cast, the casting is done by the compiler, the definition syntax is as follows:

Dynamic d=10;dynamic str= "www.zuowenjun.cn"; Dynamic p=new people ();

Note: 1. You cannot invoke an extension method directly with a dynamic type as an argument, 2. There is no implicit conversion between a delegate and a dynamic type; 3. You cannot call a constructor or static method; 4. Dynamic type dynamics cannot be declared as a base class or constrained as a generic type parameter

22. Multithreading

1. Thread-to-process relationship: a thread is the execution unit of a process, the operating system works by dispatching a thread, and the process is the container of the thread, created by the operating system, and created in the course of the execution, and a process contains at least one thread.

2. Threads are divided between foreground and background threads, and in the same thread, if all foreground threads end, the CLR forces a strong end to all background threads that are still running, and these background threads are terminated directly without throwing any exceptions. The main thread must be the foreground threads. The thread creation syntax is as follows:

Thread thread variable name =new thread ( delegate instance);

The name of the thread variable. Isbackgroud=true; True is a background thread, otherwise the front-line

The name of the thread variable. Start ();// boot thread

3. Thread pool: Refers to the collection of threads to be used in the application, and to use threads in the thread pool, the static method needs to be called: ThreadPool.QueueUserWorkItem

4. Thread synchronization: Ensure that only one thread at a time is working on shared resources, implementing thread synchronization methods:

A) monitor, lock, using the following code:

private static Object syncobj = new Object ();//static field of the class            try            {                monitor.enter (syncobj);                Other code            }            finally            {                monitor.exit (syncobj);            } Lock (Syncobj)//For the abbreviated form of the above code {  //execute code};

b) Mutex objects, mutexes are cross-process, so we can use the same mutex on multiple processes on the same machine or even a remote machine, using the code as follows:

Mutex mt=new mutex (); Mt. WaitOne ();//wait for//execute code MT. ReleaseMutex ();//Release

c) ReaderWriterLock type, when using ReaderWriterLock for resource access, if the resource does not acquire the exclusive right to write at a time, then you can obtain multiple read access, the exclusive right of a single write, if a moment has acquired the exclusive right to write, Then the other read access must wait, using the following syntax:

static ReaderWriterLock RwLock = new ReaderWriterLock ();//Class-Rwlock.acquirewriterlock (1000);// Execute code rwlock.releasewriterlock ();

D) There are also SynchronizationAttribute, MethodImplAttribute, synchronization events, and wait handles, for each of these uses can be found in this blog post:

Http://www.cnblogs.com/michaelxu/archive/2008/09/20/1293716.html

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.