11

C# 7.1 introduces a new feature called "Default Literals" that allows new default expressions.

// instead of writing
Foo x = default(Foo);

// we can just write
Foo x = default;

For Nullable<T> types, the default value is null, and with the usual usage this works as expected:

int? x = default(int?); // x is null

int? x = default; // x is null

However, when I try to use the new default literal as an optional argument (parameter) of a function, it's not working as expected:

static void Foo(int? x = default(int?))
{
    // x is null
}

static void Foo(int? x = default)
{
    // x is 0 !!!
}

To me, this behavior is unexpected and looks like a bug in the compiler.

Can anybody confirm the bug, or explain this behavior?

poke
  • 369,085
  • 72
  • 557
  • 602
Tom Pažourek
  • 9,582
  • 8
  • 66
  • 107

1 Answers1

11

After more research online, I found out that it's a known confirmed bug:

It's already fixed and will be part of C# 7.2.

Tom Pažourek
  • 9,582
  • 8
  • 66
  • 107