Using Static
With using Staticclass, you can access the static members of the Staticclass class without specifying the name of the class, and see the following example
using System.Console; namespace csharp6_0{ class usingstatic { publicvoid Printmsg (string message) { WriteLine (message); }}}
In this example, by declaring a using system.console at the outset; The WriteLine method of the console class can be accessed directly in the Printmsg method without the need to use Console.WriteLine.
One of the uses of this syntactic sugar is that for extension methods, you can introduce only an extension method in an extension class instead of introducing the entire namespace by namespace.
Index initializer
Object and collection initializers are convenient and efficient when initializing the properties of an object, a domain, or a set of initial elements of a collection, but not for dictionaries and objects with indexers. In 6.0, a new syntax was introduced for the object initializer to set value via the indexer based on key, and here is an example
classIndexerintializer { Public voidShow () {varDictionary =Newdictionary<int,string> { [0] =" First", [2] ="Third", }; Console.WriteLine (dictionary[0]); Console.WriteLine (dictionary[1]);//willthrow exception since it is not set. Console.WriteLine (dictionary[2]); } }
In the example, when creating a Dictionary object, the index initializer is used to set the value for its first and third elements.
Exception filter
Both F # and VB have exception filters, which are also included in c#6.0, so that we can write code like the one shown below
Try {...} Catch if (Myfilter (e)) {...}
The corresponding Catch statement block will be executed only if the myfilter in the If returns true, otherwise the exception will continue to be thrown. The exception filter works well when it needs to catch first and then throw out, because it does not change the stack information of the exception, and in the subsequent processing, it can take the place where the exception was originally thrown instead of being re-thrown. Let's take a look at the following example
Internal classExceptionfilter {Private voidThrowException (stringargument) { if(Argument = =NULL) { Throw NewArgumentNullException ("argument is null"); } } Private BOOLlogexcetion (Exception ex) {Console.WriteLine ("Logger:"+Ex. Message); return false; } Public voidShow () {Try{throwexception (NULL); } Catch(ArgumentNullException ex)if(Logexcetion (ex)) {Console.WriteLine ("Only print the filter return True"); } } }
This Exceptionfilter class has three methods, where ThrowException is the exception throw point, Logexception is the exception filter function, The show function calls the ThrowException function and writes the log using the Logexception function.
The following is the execution result of calling the show function
As you can see, the catch block of the show function is not executed because the Logexception function returns FALSE. The exception stack information is saved for completion.
Using await in catch and finally
In c#5.0, the await keyword cannot be used in catch and finally statement blocks, and in 6.0 the limit is gone, as shown below you can use it in catch and finally statement blocks.
Internal classawaitincatchandfinally { Public Async voidShow () {Try { awaitOpenAsync ();//You could does this . } Catch(Exception e) {awaitProcessexceptionasync (e);//Now the can do ... } finally { awaitCleanasync ();// and this } } PrivateTask Processexceptionasync (Exception e) {return NewTaskFactory (). StartNew (() = Console.WriteLine ("Processexceptionasync:"+e.message)); } PrivateTask Cleanasync () {return NewTaskFactory (). StartNew (() = Console.WriteLine ("Cleanasync is called")); } PrivateTask OpenAsync () {Throw NewException ("exception happened."); } }
In this example, the await method is used in the catch and finally statement blocks, and the following is the execution result of the program
The parameterless constructor in the structure body
Before the struct has a restriction on the constructor:
- Cannot display a constructor that declares no parameters
- The argument constructor must assign a value to all properties
In c#6.0, you can declare an parameterless constructor in a struct, and here's an example
Internal structStructparameterlessctor { Public intCpucount {Get;Set; } Public stringBand {Get;Set; } PublicStructparameterlessctor (intCOUNTOFCPU,stringband) {Cpucount=countofcpu; Band=Band; } PublicStructparameterlessctor () {Cpucount=1; Band="DELL"; } }
In this struct, there are two constructors, one with a parameter, and one with no argument, and all attributes must be assigned for both the argument and the non-argument. The parameterless constructor here can also call the constructor with the parameter as follows:
Public This (1"DELL") { }
The parameterless constructor in a struct is called only when the display uses the new operator, default(structparameterlessctor) and new structparameterlessctor[...] No parameter constructor is called.
VS2015 new features in preview c#6.0 (iii)