C# 6.0 新特性汇总
1.静态using(staticusing)
静态using声明允许不使用类名直接调用静态方法。
Thestaticusingdeclarationallowsinvokingstaticmethodswithouttheclass name. InC#5 usingSystem; Console.WriteLine("Hello,World!"); InC#6 usingstaticSystem.Console; WriteLine("Hello,World");
2.表达式方法(Expression-BodiedMethods)
使用表达式方法,只有一条语句的方法可以使用lambda语法写。
Withexpression-bodiedmethods,amethodthatincludesjustonestatementcan bewrittenwiththelambdasyntax. InC#5 publicboolIsSquare(Rectanglerect) { returnrect.Height==rect.Width; } InC#6 publicboolIsSquare(Rectanglerect)=>rect.Height==rect.Width;
3.表达式属性(Expression-BodiedProperties)
跟表达式方法类似,只有一个get访问器的单行属性可以使用lambda语法写。
Similartoexpression-bodiedmethods,one-linepropertieswithonlyagetaccessor canbewrittenwiththelambdasyntax InC#5 publicstringFullName { get { returnFirstName+""+LastName; } } InC#6 publicstringFullName=>FirstName+""+LastName;
4.自动属性初始化器(Auto-ImplementedPropertyIntializers)
自动属性可以使用属性初始化器初始化。
Auto-implementedpropertiescanbeinitializedwithapropertyinitializer.
InC#5 publicclassPerson { publicPerson() { Age=24; } publicintAge{get;set;} } InC#6 publicclassPerson { publicintAge{get;set;}=42; }
5.只读自动属性(Read-OnlyAutoProperties)
C#5需要完整的属性语法实现只读属性,C#6可以使用自动属性实现。
Toimplementread-onlyproperties,C#5requiresthefullpropertysyntax.With C#6,youcandothisusingauto-implementedproperties. InC#5 privatereadonlyint_bookId; publicBookId { get { return_bookId; } } InC#6 publicBookId{get;}
6.nameof操作符(nameofOperator)
字段、属性、方法和类型的name可以通过nameof访问。使用nameof,可以方便的重构name变化。
Withthenewnameofoperator,namesoffields,properties,methods,ortypescan beaccessed.Withthis,namechangesarenotmissedwithrefactoring. InC#5 publicvoidMethod(objecto) { if(o==null)thrownewArgumentNullException("o"); InC#6 publicvoidMethod(objecto) { if(o==null)thrownewArgumentNullException(nameof(o));
7.Null传递操作符(NullPropagationOperator)
Null传递操作符简化了空值检查。
Thenullpropagationoperatorsimplifiesnullchecks. InC#5 int?age=p==null?null:p.Age; varhandler=Event; if(handler!=null) { handler(source,e); } InC#6 int?age=p?.Age; handler?.Invoke(source,e);
8.字符串插值(StringInterpolation)
字符串差值移除了对string.Format的调用,使用表达式占位符取代数字格式占位符。
Thestringinterpolationremovescallstostring.Format.Insteadofusing numberedformatplaceholdersinthestring,theplaceholderscaninclude expressions. InC#5 publicoverrideToString() { returnstring.Format("{0},{1}",Title,Publisher); } InC#6 publicoverrideToString()=>$"{Title}{Publisher}";
9.字典初始化器(DictionaryInitializers)
字典可以使用类似集合的字典初始化器初始化。
Dictionariescannowbeinitializedwithadictionaryinitializer—similartothe collectioninitializer. InC#5 vardict=newDictionary<int,string>(); dict.Add(3,"three"); dict.Add(7,"seven"); InC#6 vardict=newDictionary<int,string>() { [3]="three", [7]="seven" };
10.异常过滤器(ExceptionFilters)
异常过滤器允许你在捕获异常前进行过滤。
Exceptionfiltersallowyoutofilterexceptionsbeforecatchingthem.
InC#5 try { //etc. }catch(MyExceptionex) { if(ex.ErrorCode!=405)throw; //etc. } InC#6 try { //etc. }catch(MyExceptionex)when(ex.ErrorCode==405) { //etc. }
11.在Catch使用Await(AwaitinCatch)
await可以在catch块中直接使用,C#5中需要变通使用。
awaitcannowbeusedinthecatchclause.C#5requiredaworkaround. InC#5 boolhasError=false; stringerrorMessage=null; try { //etc. }catch(MyExceptionex) { hasError=true; errorMessage=ex.Message; } if(hasError) { awaitnewMessageDialog().ShowAsync(errorMessage); } InC#6 try { //etc. }catch(MyExceptionex) { awaitnewMessageDialog().ShowAsync(ex.Message); }
以上所述是小编给大家介绍的C#6.0新特性汇总,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!