0

Here is a minimal sample:

class C {
  public bool F<T>(T x) => x is null;
}

The real code causing the issue is here https://github.com/kofifus/With/blob/master/With.cs#L35

Until today this compiled fine, but I just upgraded to

Microsoft Visual Studio Community 2019 Preview Version 16.1.0 Preview 2.0

The code now errors with:

Error CS8511 An expression of type 'T' cannot be handled by a pattern of type '<null>'. Please use language version 'preview' or greater to match an open type with a constant pattern.

What's going on here ? and what is the correct way to solve this ?

note1 - I don't want C# language preview ATM
note2 - It still compiles if I change x is null to x==null

13
  • Until today this compiled fine really? hrmmm, my spidy senses are tingling
    – TheGeneral
    Apr 23, 2019 at 23:45
  • It doesn't compile for me in an older environment - Are you sure your code does compile in an older environment? You tested just before posting? Apr 23, 2019 at 23:49
  • What are you trying to accomplish? Apr 23, 2019 at 23:51
  • I did. The real code is long so I went with an example. You can see it here - github.com/kofifus/With/blob/master/With.cs#L35 - that compiled through all VS19 previews 1.x but stopped working for preview 2.0
    – kofifus
    Apr 23, 2019 at 23:51
  • Can you use the negation of is object ? Apr 23, 2019 at 23:56

2 Answers 2

5

Adding to the solution mentioned, the fact that this used to be working for you is likely to be caused by a bug in the 2019 preview.

However, in Visual Studio 2019 we improperly permitted this to compile in language versions 7.0, 7.1, 7.2, and 7.3. In Visual Studio 2019 Update 1 we will make it an error (as it was in Visual Studio 2017), and suggest updating to preview or 8.0.

Seems like allowing is null in open generics will be part of C#8 and is championed here. The error is likely to eventually tell you to upgrade to C#8 (instead of 'preview') after it's released.

1
  • This is correct. t is null previously was disallowed by the language. We implemented the change to allow it in C# 8.0, but accidentally forgot to limit the change to that language version. This was fixed in Visual Studio 2019 update 1 and documented as a break (in case anyone had taken a dependency on that behavior). Apr 24, 2019 at 2:23
1

I highly doubt your assertion this always worked.

Update

If it did work in a previous preview, it was probably due to a language feature that got dumped

Possibly you are trying to compare with null i.e ==

public bool F<T>(T x) => x == null;

However, a more robust way of checking generics for equality is with EqualityComparer<T>.Default. This respects IEquatable<T> without boxing as well as object.Equals, and handles all the Nullable<T> and lifted nuances of nullable types

public bool F<T>(T x) => EqualityComparer<T>.Default.Equals(x, default(T));

Update

Just for the record, (as you can see here)

public bool F<T>(T x) => !(x is object);

Basically just compiles to

public bool F<T>(T x)
{
    return x == null;
}

Which is the same as

public bool F<T>(T x) => x == null;
7
  • strange it did used to work .. the real line was here github.com/kofifus/With/blob/master/With.cs#L35
    – kofifus
    Apr 23, 2019 at 23:54
  • wow that's quite a mouthful, can you explain why is null doesn't work here ?
    – kofifus
    Apr 23, 2019 at 23:55
  • @kofifus its just a more robust generic check, and works against the default value of the type, it also saves some IL due to boxing value types, however this does depend on your use case, and what level of null or default you want.
    – TheGeneral
    Apr 24, 2019 at 0:03
  • Thx! what level of null or default you want - what do you mean ? I just want ot check for null. Can you explain further or tell me where to find more info ? Also what is the difference between above and if (!(x is object)) which works as well ?
    – kofifus
    Apr 24, 2019 at 0:05
  • 1
    is checks against a type, and null is not a type, it's a value. Just like you can't do x is 5. You could do x is string, though.
    – Rufus L
    Apr 24, 2019 at 0:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.