Transferred from: http://www.cnblogs.com/knowledgesea/p/6694979.html
Preface
Since the release of the early 2000, the C # programming language has been continuously improved, allowing us to write code more clearly, and easier to maintain our code, enhanced functionality has been achieved from 1.0 to 7.0 or even 7.1, each change with the corresponding support of the. NET Framework Library, Also constantly bring to our expectations and surprises. Here we go to C # all the way to the present, to do a review and study.
C # language goals and forward
C # is designed with the following points in mind:
Designed to be a simple, modern, universal object-oriented programming language.
Language and its implementation should provide support for software engineering principles such as strong type checking, array dimension checking, uninitialized variable reference detection, and automatic garbage collection. Software robustness, durability, and programmer productivity are important.
Designed to develop software components that are suitable for deployment in a distributed environment.
Portability is important for source code and programmers, especially programmers who are already familiar with C and C + +.
It is very important to support internationalization.
C # is ideal for writing applications for managed and embedded systems, from the use of complex operating systems to very small, specialized features.
While C # applications are economical in terms of memory and processing power requirements, the language is not intended to directly compete with performance and size directly in C or assembly language.
C # version
Features added in the C # version:
c#2.0
Generic type
Partial types
Anonymous methods
Iterators
Nullable types
Getter/setter Individual Accessibility
Method Group conversion (REP)
Co-and Contra-variance for delegates
Static class
Delegate Inference
c#3.0
Implicitly-typed Local variables
Objects and collection initializers
Auto-implemented Properties
Anonymous type
Extension methods
query expressions
Lambda expression
Expression tree
Partial methods
c#4.0
Dynamic binding
Named and optional parameters
Generic Co-and contravariance
Embedded interop Types ("Nopia")
c#5.0
Async methods
Caller Info Attributes
c#6.0
Compiler-as-a-service (Roslyn)
Importing a static type member into a namespace
Exception filter
Using await in catch and finally
Automatic property initializer
Default value for read-only properties
Expression-bodied Members
Null-conditional operators (empty conditional operator, simple check)
string interpolation
- Nameof operator
Dictionary initializer
c#7.0
Out variable
Pattern matching
Meta-group
Deconstruction
Local functions
Number Separator
Binary text
Local references and references returned
Extended Asynchronous return type
Constructor and finalizers of an expression
- Expression bodied getters and setters
Throw-expression
c#7.1
- Async Main
Default expression
Reference Document: Https://en.wikipedia.org/wiki/C_Sharp_ (programming_language) #cite_note-roslyn_6-47
c#7.0 new features detailed
In the 2016 visual Studio "4" Preview release, C # 7.0 began to emerge, now vs2017 has been released, c#7.0 brings us the surprise, we should also explore.
c#7.0 adds many new features, focusing on data, code simplification, and performance.
Out variables
Currently in C #, using out parameters is not as smooth as we want. Before invoking a method with an out parameter, you must first declare the variable to pass to it. You also cannot use var them to declare them, but you need to specify the complete type.
public void Printcoordinates (point p) { int x, y;//must declare p.getcoordinates (out x, out y); WriteLine ($ "({x}, {y})");
Take a look at the c#7.
public void Printcoordinates (point p) { p.getcoordinates (out int x, out int y); WriteLine ($ "({x}, {y})");
Pattern matching
In C#7
public static void Printstars (object o) { if (o is int i) Console.WriteLine (i +); }
Previous versions need to be converted
public static void Printstars (object o) { if (o is int) Console.WriteLine (Convert.ToInt32 (o) +); }
Switch statements with patterns extended switch statement using pattern matching
public static void Printstars (object o) { switch (o) {case- Print p: Break ; case int A: break ; Case String B is b== "123": Break ; }} public class Print {public string Printname {get; set;} public string Mobanpath {get; set;} public int Count {get; set;} }
Tuples (tuples)
Note : Tuples depend on a set of underlying types and are not included in preview 4. For the functionality to work properly, you can easily get them from NuGet:
Right-click the project in Solution Explorer and select Manage NuGet packages ...
Select the Browse tab, select Include pre-release, and then select Nuget.org as the package source
Search for "system.valuetuple" and install it.
static void Main (string[] args) { var tuple = (A:10, B: "123"); Console.WriteLine ($ "a:{tuple.a},b:{tuple.b}"); var result1 = GetS (); var result = Get (); Console.WriteLine ($ "ITEM1:{RESULT1. Item1},item2:{result1. Item2},item3:{result1. ITEM3} "); Console.WriteLine ($ "a:{result.a},b:{result.b},c:{result.c}"); Console.ReadLine (); } static (string, int, DateTime) GetS () { return ("ABC", 123, DateTime.Now); } Static (string A, int B, DateTime c) Get () { return (a: "abc", b:123, C:datetime.now); }
Local functions
Simply put, it is the method that writes the method and then calls itself.
Note: In preview 4, the local function must be declared before the call. This restriction will be released, so once the local variables they read are explicitly assigned, they can be called.
static void Main (string[] args) { Console.WriteLine ($ "{Get (123)},{get (" abc ")},{get (NULL)}"); Console.ReadLine (); } public static string Get (object a) { return getp (); String Getp () { if (A is int v) return v + ""; If (A is string b) return B; return "CCC"; } }
Literal improvements
c#7.0 allows you to _ appear as a number separator in numeric literals:
var d = 123_456;var x = 0xab_cd_ef;
You can put them between numbers to improve readability. They have no effect on value.
In addition, c#7.0 introduces binary text, so you can directly refer to the positioning mode without having to know the hex symbol with the heart.
var B = 0b1010_1011_1100_1101_1110_1111;
More Features of c#7.0
Here, I will not be staged c#7.0 of the other functions. If you want to delve into other, read the following document carefully:
Please refer to the documentation: HTTPS://BLOGS.MSDN.MICROSOFT.COM/DOTNET/2016/08/24/WHATS-NEW-IN-CSHARP-7-0/
c#6.0 New language Features
The following new features are implemented and available in VS 2015 and 17.
Add: Added, Exists: Existing, N/A: no meaning for the language, no: This version is not available.
I'll show you a couple of new features, and I'll take a link to the document and watch it.
string interpolation
This is reflected in the code above.
var s = $ "{P.name} is {p.age} year{{s}} old";
Dictionary index Initialization
var numbers = new Dictionary<int, string> { [7] = "Seven", [9] = "Nine", [[] = "Thirteen"};
Automatic attribute Initialization
public class customer{Public string First {get; set;} = "Jane"; public string Last {get; set;} = "Doe";}
Using References static classes
Using static system.console;using static system.math;using static system.dayofweek;class program{ static void Main ( ) { WriteLine (Sqrt (3*3 + 4*4)); WriteLine (Friday-monday); }}
More c#6.0 languages new features
The online code can perform a demo document connection: The http://www.volatileread.com/Wiki/Index?id=1075 effect is as follows
Reference Document: Https://github.com/dotnet/roslyn/wiki/New-Language-Features-in-C%23-6
c#2.0-5.0 Reference Document c#5.0 Reference document
Connection Address: https://blogs.msdn.microsoft.com/mvpawardprogram/2012/03/26/an-introduction-to-new-features-in-c-5-0/
c#4.0 reference Documentation
Connection Address: https://msdn.microsoft.com/en-us/magazine/ff796223.aspx
c#3.0 reference Documentation
Connection Address: https://msdn.microsoft.com/en-us/library/bb308966.aspx
c#2.0 reference Documentation
Connection address: https://msdn.microsoft.com/en-us/library/7cz8t42e (v=vs.80). aspx
The new features in the "Go" C # editions are detailed