Is this pattern matching expression equivalent to not null
Asked Answered
D

3

20

I stumbled upon this code on github:

if (requestHeaders is {})

and I don't understand what it does exactly.

Upon experimenting it's seems to only be false when requestHeaders is null.

Is this just another way of writing if (requestHeaders != null) or if (!(requestHeaders is null))?

Declarant answered 16/12, 2019 at 17:17 Comment(1)
It checks that requestHeaders is anonymous object and has any value except null, actually you are right with null checksGahl
U
17

The pattern-matching in C# supports property pattern matching. e.g.

if (requestHeaders  is HttpRequestHeader {X is 3, Y is var y})

The semantics of a property pattern is that it first tests if the input is non-null. so it allows you to write:

if (requestHeaders is {}) // will check if object is not null

You can write the same type checking in any of the following manner that will provide a Not Null Check included:

if (s is object o) ... // o is of type object
if (s is string x) ... // x is of type string
if (s is {} x) ... // x is of type string
if (s is {}) ...

Read more here.

Unbend answered 16/12, 2019 at 17:29 Comment(4)
Is there any reason to use this pattern rather than if (!= null) ?Declarant
In this case, I don't think it's any different. It's same as using !=null.Unbend
I think the pattern must be HttpRequestHeader {X: 3, Y: var y} where X and Y are properties of HttpRequestHeader. I recently used p is IImage { Image: { } img } imageItem which is equivalent to p is IImage imageItem && imageItem.Image != null and the image is assigned to img at the same time. You could also simply write p is IImage { Image: { } img } if you only need img and don't need imageItem.Earthstar
how do you know this information? this is not present in any official documentation. the only place this info appears is here on stack overflow.Spermatogonium
K
6

As an addition to @vendettamit's answer:

Since C# 9, it is the same as writing

if (requestHeaders is not null)

which does not require any further explanation

Kristelkristen answered 3/8, 2022 at 14:28 Comment(0)
W
1

One difference between != null and is { } is that the first can be overridden whereas the second cannot.

For example, if you paste the following into a new Console app and run it

DoNotDoThis? a = new(); // a isn't null

Console.WriteLine("a == null: {0}", a == null);
Console.WriteLine("a != null: {0}", a != null);
Console.WriteLine();
Console.WriteLine("a is null: {0}", a is null);
Console.WriteLine("a is not null: {0}", a is not null);

public class DoNotDoThis
{
    public static bool operator ==(DoNotDoThis? a, DoNotDoThis? b)
    {
        return ReferenceEquals(b, null);
    }

    public static bool operator !=(DoNotDoThis? a, DoNotDoThis? b)
    {
        return !(a == b);
    }
}

you get the following output

a == null: True
a != null: False

a is null: False
a is not null: True

Interestingly, ReSharper gives you a warning on the first line:

Expression is always false

Wentz answered 2/5 at 11:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.