C# Futures: Null Check Operator

栏目: IT技术 · 发布时间: 3年前

内容简介:In theThis eliminates the problem of the single-bang having two similar, but opposite meanings.In the above example,

In the June 24th C# Language Design Meeting , Microsoft made a subtle change to the parameter null checking syntax . By changing it from Type parameterName! to Type parameterName!! they effectively introduced a new ‘null check operator’.

This eliminates the problem of the single-bang having two similar, but opposite meanings.

void MethodA(string a!)

string a = b!;

In the above example, a! would mean “verify this isn’t null” while b! means “assume this isn’t null without checking”.

Before settling on the double-bang, there were other contenders for the syntax.

void M(string param!)
void M(string param!!)
void M(string! param)
void M(string !param)
void M(checked string param)
void M(string param ?? throw)
void M(string param is not null)
void M(notnull string param)
void M(null checked string param)
void M(bikeshed string param)
void M([NullChecked("Helper")] string param)
/* contract precondition forms */
void M(string param) Requires.NotNull(param)
void M(string param) when param is not null

For those wondering about the ‘bikeshed’ keyword, it refers to Parkinson's Law of Triviality where in a committee is likely to focus the majority of their attention on minor details, say the bike shed outside of a nuclear reactor, while ignoring the hard questions such as which reactor core to use. One has to assume the note-taker for the June 17 C# Language Design Meeting was getting a bit frustrated.

For the C# 9 timeframe, the null check operator ( !! ) will still only be applicable to parameters. But developers and language designers are already speculating on where else it could be used.

To illustrate, consider this function and statement.

string? Foo();
string x1 = Foo();

This would result in a compiler warning if nullable reference types are enabled. But the developer may know that Foo() won’t return a null in this context. So the developer silences the exception with a bang ( ! ):

string x2 = Foo()!;

This says, “Assign the result of Foo() to x2 . Assume that Foo() won’t return a null.”

That may be acceptable today, but if Foo() changes in the future it may start returning unexpected nulls. To address this, a future version of C# may allow a double-bang ( !! ):

string x3 = Foo()!!;

This changes the meaning to be, “Throw an exception if Foo() returns a null, otherwise assign the result to x3 .”

Initial thoughts would be the exception thrown would probably be an InvalidOperationException with the currently executing expression being included in the exception’s message. This is problematic, however, as the type of the exception should indicate if the error was made by the caller, the library, or the environment . An InvalidOperationException implies the caller made a mistake, which in most cases this is more of “library author made the wrong assumption” scenario.

Ed Ball asks,

Could the !! operator translate into Value access for Nullable<T> ?

void MethodOne(string? text, DateTime? dateTime)
{
    MethodTwo(text!!.Substring(1)); // throws InvalidOperationException if null
    MethodThree(dateTime!.Value.AddHours(1)); // throws InvalidOperationException if null
    MethodThree(dateTime!!.AddHours(1)); // throws InvalidOperationException if null
}

以上所述就是小编给大家介绍的《C# Futures: Null Check Operator》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

天使投资

天使投资

唐滔 / 浙江人民出版社 / 2012-4-30 / 56.99元

1.国内首部天使投资的实战手册,堪称创业者的第一本书,打造创业者与天使投资人沟通的最佳桥梁。 2. 薛蛮子、徐小平、雷军、周鸿祎、孙陶然、但斌、曾玉、查立、杨宁、户才和、周哲、莫天全、《创业家》、《创业邦》等联袂推荐。 3.作者唐滔结合他在美国和中国17年的创业和投资经历,为创业者和投资者提供了珍贵和可靠的第一手资料。 4.创业者应何时融资?花多少时间去融资?如何获得融资者青睐?......一起来看看 《天使投资》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

在线进制转换器
在线进制转换器

各进制数互转换器

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具