Original address: Http://www.oschina.net/translate/briefly-exploring-csharp-new-features
A few weeks ago I read about some of the new features of c#6 in different places. I decided to collect them all together, and if you haven't read them, you can go through them all at once. Some of them may not be as magical as expected, but that's just the current update.
You can get them by downloading VS2014 or installing the Roslyn Package for visual studio2013 here.
So let's take a look at it:
1. $ sign
The purpose of using it is to simplify the index-based string, that's all. It is not like some of the dynamic features of C # now, because it uses the normal indexing function internally. To compile the understanding, see the following example:
varCol =Newdictionary<string,string>() {$first="Hassan" }; //Assign value to member//The old by :Col. $first ="Hassan"; //The NEW:col[" First"] ="Hassan";
2. Exception filter:
The exception filter has been supported by the VB compiler and is now being introduced in C #. The exception filter allows you to specify a condition for a catch block. This catch block will only be executed when the condition is met, which is my favorite feature, so let's take a look at the example:
Try { Throw NewException ("Me"); } Catch(Exception ex)if(ex. Message = =" You") { //This one would not execute. } Catch(Exception ex)if(ex. Message = ="Me") { //This one would execute}
3. The await keyword in catch and finally blocks
As far as I know, no one knows why the await keyword is not available in the catch and finally blocks in C # 5, no matter what the wording is, it is unavailable. That's good. Because developers often want to see the I/O operations log, in order to record the caught exception information into the log, it needs to be done asynchronously.
Try { dosomething (); } Catch (Exception) { await Logservice.logasync (ex); }
4. Declaring an expression
This feature allows the developer to define a variable in an expression. This is simple but practical. I used to do a lot of Web sites, the following is my common code:
Long ID; if (! long. TryParse (request.qureystring["ID" out id)) {}
Optimized code:
if (! long. TryParse (request.qureystring["ID"outlong ID)) {}
5. Use of static
This feature allows you to specify a specific type in a using statement, after which all static members of this type can be used in subsequent clauses.
1 usingSystem.Console;2 3 namespaceConsoleApplication104 {5 class Program6 {7 Static voidMain (string[] args)8 {9 //Use WriteLine method of the Console classTen //without specifying the class name OneWriteLine ("Hellow World"); A } - } -}
6. Automatic initialization of attributes:
The C # 6 auto-comfort property is like a domain in a declared position. The only thing to know here is that this initialization does not cause the setter method to be called internally. The field values in the background are set directly, and the following is an example:
Public classPerson {//can use the feature on both//getter only and Setter/getter only properties Public stringFirstName {Get;Set; } ="Hassan"; Public stringLastName {Get; } ="Hashemi"; }
7. Main constructor:
Call Haha, the main constructor will help you eliminate this pain in getting the constructor parameters and setting it to the class's domain to support the latter operation. This is really useful. The primary purpose of this feature is to initialize using constructor parameters. When the primary constructor is declared, all other constructors need to use: this () to invoke the main constructor.
Finally, here's an example:
// This is the primary constructor: class Person (stringstring lastName) { publicstringget set; } = firstName; Public string LastName get;} = LastName; }
Note that the call to the main constructor is at the top of the class.
[Go] Brief introduction to the new features of C # 6