Tom*_*rek 11 c# default nullable compiler-bug c#-7.1
C#7.1引入了一个名为"默认文字"的新功能,它允许使用新的default表达式.
// instead of writing
Foo x = default(Foo);
// we can just write
Foo x = default;
Run Code Online (Sandbox Code Playgroud)
对于Nullable<T>类型,默认值为null,并且通常使用它按预期工作:
int? x = default(int?); // x is null
int? x = default; // x is null
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试使用新的默认文字作为函数的可选参数(参数)时,它不能按预期工作:
static void Foo(int? x = default(int?))
{
// x is null
}
static void Foo(int? x = default)
{
// x is 0 !!!
}
Run Code Online (Sandbox Code Playgroud)
对我来说,这种行为是意外的,看起来像编译器中的一个错误.
任何人都可以确认错误,或解释这种行为?
| 归档时间: |
|
| 查看次数: |
654 次 |
| 最近记录: |