内容简介:When we last reported onThe rules are pretty straight forward.Regarding the constructor rule, Microsoft did consider allowing the parameter check to occur before invoking the base class constructor. This has always been allowed by .NET and is arguably pref
When we last reported on Simplified Parameter Null Validation , this feature was being challenged by several competing proposals ranging from attributes and compilers flags to full scale AOP with IL weaving. All of that has been discarded in favor of a narrowly tailored feature proposal which reduces the amount of code needed to validate non-null parameters to a single character.
The bang operator, !, can be positioned after any identifier in a parameter list and this will cause the C# compiler to emit standard null checking code for that parameter. For example:
void M(string name!) {
...
}
Will be translated into:
void M(string name!) {
if (name is null) {
throw new ArgumentNullException(nameof(name));
}
...
}
The rules are pretty straight forward.
== string? x!
Regarding the constructor rule, Microsoft did consider allowing the parameter check to occur before invoking the base class constructor. This has always been allowed by .NET and is arguably preferable. However, the C# syntax doesn’t expose this capability, and they didn’t want to generate code that couldn’t be created without using this feature.
In the test plan , we see these scenarios for generics
void M<T>(T value!) { } is OK
void M<T>(T value!) where T : struct { } is an error
void M<T>(T value!) where T : unmanaged { } is an error
void M<T>(T value!) where T : notnull { } is OK
void M<T>(T value!) where T : class { } is OK
void M<T>(T value!) where T : SomeStruct { } is an error
void M<T>(T value!) where T : SomeClass { } is OK
One flaw in the design is that it offers no support for validating properties, as the value parameter is only implied. Orthoxerox suggested a work-around where the bang operator is applied to the set keyword in this scenario.
public Foo Foo {get; set!;}
public Bar Bar {
get { return bar; }
set! { bar = value; DoSomethingElse(); }
}
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
一个APP的诞生
Carol 炒炒、刘焯琛 / 电子工业出版社 / 2016-7-1 / 79
在移动互联网高度发达的今天,一个个APP,成为我们通向网络世界的窗口。它的诞生流程,令不少对互联网世界产生幻想甚至试图投身其中的年轻人充满了好奇。 《一个APP 的诞生》就是这样一步一步拆分一个APP 的诞生过程。从前期市场调研,竞品分析开始,一直到设计规范,界面图标,设计基础,流程管理,开发实现,市场推广,服务设计,甚至跨界融合,都有陈述。 《一个APP 的诞生》被定义是一本教科书,......一起来看看 《一个APP的诞生》 这本书的介绍吧!