Popular Microsoft Knowledge points-C #3.0 New Features

Source: Internet
Author: User
Document directory
  • Extension methods)
  • Anonymous expressions (lambda expressions)
  • Object and collection initializers)
  • Anonymous type (anonymous types)
  • Query expressions)
C #3.0 Davies published on | category: programming ::

Some people posted the introduction of C #3.0 in the DOTNET version. I 've probably looked over it. There are a lot of surprises. C # introduces the features of many other scripting languages and is more and more suitable for rapid development, or even prototype development. Briefly summarized as follows:

Implicitly typed local variables)

As in Javascript, VAR I = 0 and var s = "string" are equivalent to int I = 0 and string S = "string ". However, empty type, dynamic type, and initial hermit Declaration are not supported. This is actually a static type, but it does not improve much, but it is more "Dummies" When declaring variables. A completely dynamic language like python can be used very well. For example, it is very convenient to put different types of data in the same array or collection, but there may be many potential problems, developing large systems is not suitable.

Extension methods)

You can declare a static function. The first parameter of the function is modified with this, and then the object of the first parameter type can be used as a member function. For example, a function is public static int str2int (this string S), which is usually used as str2int ("string"), but now "string" can be used ". str2int () syntax. This is very similar to Python, Ruby, and other languages.

Anonymous expressions (lambda expressions)

It is not appropriate to declare a function when writing a very small function for the time being. Anonymous functions can solve such problems well and are very suitable for functional programming. In C #3.0, anonymous expressions are also supported. The => operator is used. The parameter list on the left and function body on the right are used to complete a function in a row. For example, x => x + 1 is equivalent to int func (int x) {return x + 1 ;}. obviously, the former is very concise and intuitive. It is very convenient to use anonymous functions to operate datasets. For example, filter (list, x => x> 5) can extract the number larger than 5 in list. Anonymous functions are well supported in Python, list, and other languages. The anonymous function in C # is an extension of Delegate. It is also strongly typed. For example, it is wrong to define func <double, int> F = x => x + 1.

Object and collection initializers)

The parameter list can be displayed during object initialization, for example, point P = new point {x = 1, y = 2};, so that the written code is highly readable. But I don't know how to assign a value to the parameter when calling a function. It is acceptable in Python. For example, a function has 10 parameters and is optional. I want to set a non-default value for the last parameter. In C ++, all and parameters must be provided, and the first nine parameters must be the default values. In python, you can directly specify a value for a parameter in the parameter list, such as open_html_page ("http://blog.daviesliu.net/", proxy = "proxy.rainbud.net "). Arrays can be initialized in the form of a list, such as list <int> List = new list <int> {1, 2, 4 };

Anonymous type (anonymous types)

The member variables in the class can also be implicitly declared. You do not need to specify the type when declaring the class. The type of the member variables is determined by the parameter type during initialization. This is actually equivalent to a template in C ++. Every member variable has a parameter type. When a certain type is given to get the instance, a class that meets this type will be instantiated. This is another improvement after C #2.0 supports model programming. The General Collection class in C #2.0 is called generic collections, for example, the list used in the preceding example <>. It is said that this template is highly efficient, and even the list <int> should be higher than the self-implemented intlist.

Query expressions)

This is a very creative part of C #3.0. It actually inherits the SQL syntax used for database query to the advanced programming language, so that it will be very convenient to develop database programs in the future, it is possible to operate the database as easily as operating local variables. Keywords such as from where oderby select are used. For example, from C in customers orderby C. name where c. city = "Beijing" select new {C. name, C. you can select the customers in Beijing and sort them by name. Only the names and ages are displayed. This programming method is very cool. I don't know what the performance is, so it should not be low.

C #2.0 does not seem to have been officially released yet. The 3.0 standard has come out, and there are many highlights worth looking forward. We can see that C #3.0 draws on the features of many scripting languages, such as Python. Maybe Microsoft has dug many experts from the open-source community to design C #.

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.