Online Course Notes & mdash;. NET basics, course notes. net

Source: Internet
Author: User

Online Course Notes-. NET basics, course notes. net

Take notes on the online course of Jin xuliang from Beijing University of Technology.

Introduction:

Online Course URL: http://mooc.study.163.com/university/BIT#/c

Instructor's personal website: http://jinxuliang.com/MainWebSite

The course class No. 10.10 consists of three main courses, of which I want to learn the latest course about ASP. NET Core. Now I have introduced this chapter. I learned it yesterday and it is very beneficial (although my environment has not been installed, my computer configuration is an injury ). It is helpful for those who want to know Core-related technologies. (At the very least, there is a reference), followed by instructions on dependency injection, IOC containers, AngularJS, and DDD (advanced knowledge ). It is said that the instructor's website was created through ASP. NET Core technology.

Dynamic type

This type is often used in previous projects, but this knowledge point is not very clear. Today, I learned again.

The dynamic type is a dynamic type, which is different from the conventional one. It is a special type that can change the value of a variable. Just like the following code.

// Dynamic is a dynamic type that can be directly changed during compilation. Instead of the regular type. You cannot change the value. Dynamic test = "test1"; Console. WriteLine (test. GetType (); test = 23; Console. WriteLine (test. GetType ());

The output is two different types. in ASP. net mvc, ViewBag uses the dynamic type. (You can't think of it>)

The following example adds a delegate method to this type. You can also output methods.

Dynamic myBag = new ExpandoObject (); myBag. intValue = 100; myBag. message = "hello"; // declare the delegate method. str is equivalent to the parameter of the method. The value in {} is the method body. Action <string> act = (str) => {Console. writeLine (str) ;}; // assign the method to the say method; myBag. say = act; Console. writeLine (myBag. intValue * 10); Console. writeLine (myBag. message); myBag. say (myBag. intValue. toString (); // The method parameter is string type myBag. say (myBag. message );

The ExpandoObject () object is declared. in Dynamic, there is a method in it, which indicates an object, which contains members (including attributes and methods) that can be dynamically added and removed at runtime ). The method is to initialize a new ExpandoObject that does not contain any members.

Delegate and Lambda expressions

Let's first look at how some common delegation is implemented.

Use of delegation methods:

// 1: declare the delegate public delegate string SomeDelegateType (int value); class MyClass {// 2: Define the delegate method public string process (int value) {return value. toString ();}}

MyClass ob = new MyClass (); // 3: Declares the delegate variable and assigns a value to it. SomeDelegateType del = ob. process; // 4: Call the delegate. This del is actually equivalent to the process () method. Console. WriteLine (del (100 ));

The transformed Lambda expression:

// 1: declare the delegate public delegate string SomeDelegateType (int value );
   SomeDelegateType del = (value) =>value.ToString();
   Console.WriteLine(del(100));

Comparison:

With Lambda expressions, you do not need to write so much code, but do not need to define the actual delegate method. Variables do not need to be instantiated. In fact, if you use the built-in delegate type, you do not need to define the delegate;

            Func<int,string> del = (value) =>value.ToString();            Console.WriteLine(del(100));

Lambda expressions:

Task Parallel Library (TPL: Task Parallel Library)

TPL is a powerful tool for developing high-performance. NET applications and the cornerstone of asynchronous programming for. Net programs. Learning it can better learn async/await asynchronous programming mode;

  • The key to developing parallel programs is to find a proper job decomposition solution;
  • Parallel Processing always has to pay a certain price, such as thread synchronization, thread communication, and synchronous data buffering.
  • Parallel algorithms must be carefully designed and tested in multiple software and hardware environments.

Parallel Computing component in. NET platform

Comparison between multithreading and serial programs

Think about why multi-threaded programming occurs. It is mainly for a reasonable reason that the computer's CPU resources will not be idle, and more things will be handled within a limited time to improve resource utilization.

Improving the abstraction layer is an effective way to reduce the difficulty of software development and improve the efficiency of software development;

 

Parallel development of Parallel computing programs

Paraller has three built-in typical code execution procedures

  • Parallel. Invoke (); Parallel execution mode;
  • Parallel. For (); Parallel loop;
  • Parallel. ForEach (); and iteration;

This Parallel relies on the underlying Task class to complete the work. An instance of the Task class represents a job that can be executed. It is a heap type in the "Task parallel library;

The development of parallel computing programs based on TPL involves almost all tasks.

Background thread

  • The background thread ends after the main thread ends;
  • Asynchronous calls are executed by threads later than the thread;
  • Await statements do not block caller threads;
  • The code in the same method uses await as the boundary and is divided into two blocks (or multiple blocks, depending on the number of await statements). Then, A thread in the thread pool is responsible for executing them.
  • Async/await is actually a syntactic sugar. Its functions can be fully implemented by TPL or directly implemented by threads. The advantage of Using async/await is that it can write concise code;

Applicable scenarios of async/await
Multi-threaded desktop applications;
Intensive I/O operations on the server;

Create and start parallel computing tasks

1: You can directly create a Task object using the new keyword. The Task constructor receives an Action-type delegate to encapsulate the functional code that requires parallel execution;

After the task object is created, call its Start () method to Start the task. The task will be appended to the task list in the thread pool for scheduling and execution.

     t.Start();

2: You can combine the creation and running of Task objects;

Task t = Task. Factory. StartNew () => {Task method });
     Task s = Task.Run(()=>{});

Returns a "latency" task.

The Task. Delay () method returns a Task object, which is mainly used in an Asynchronous Method with await/async. It is characterized by not blocking the caller thread. This is used in actual development.

       await Task.Delay(5000);

Demo

Private static void UseTask () {Console. writeLine ("use Task. the Delay () method slows down the Running Speed "); // instantiate the function through delegation to operate asynchronously. run () => {for (int I = 1; I <= 10; I ++) {Console. writeLine ("{0}", I); Task. delay( 500 ). wait (); // thread Wait }}). wait ();}

Retrieves the running result of a Task.

  • Use traditional multi-threaded programming technology and use the thread to synchronize objects to retrieve results;
  • Use Task <T>. Result to retrieve the Result in blocking wait mode;
  • Use the Task. ContinueWith () method to retrieve the result in callback mode;

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.