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. $ identifier
The function of $ is to simplify the string index. It differs from those in C # that use regular expression matching to implement indexes in the dynamic nature of an index. Examples are as follows:
var col = new dictionary<string, string> () { $first = "Hassan" }; Assign value to member //the old: col. $first = "Hassan"; The NEW: col["first"] = "Hassan";
2. Exception filter
The VB compiler has already supported exception filters, and now C # is supported. An exception filter allows a developer to create a catch block for a condition. The code in the CATCH block executes only when the condition is met, which is one of my favorite features, as shown in the following example:
Try { throw new Exception ("Me"); catch (Exception ex) if (ex. Message = = "You")
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"], out long id)) {}
The scope of the variables in this declarative way is the same as C # when declaring variables in a general way.
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.
Using System.console;namespace consoleapplication10{ class program { static void Main (string[] args) { //use writeLine method of Console class //without specifying the class name writeLine ("Hellow World") ; } }}
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 class person { //You can use this feature on both //getter only and Setter/getter only properties< C12/>public string FirstName {get; set;} = "Hassan"; public string LastName {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:
The primary constructor: Class person (String firstName, String lastName) {public string firstName {get; set;} = FirstName; public string LastName {get;} = LastName; }
A brief introduction to the new features of C # 6. NET 6 is coming?