-1

What are these empty double brackets doing in this expression?

record MyRecord(int i, int? j);

public void MyMethod()
{
    MyRecord myVar = new(10);

    if (myVar is { i: { } })
    {
          // ?
    }

    if (myVar is { j: { } })
    {
          // ?
    }
}

I didn't find in the reference pages.

Is this just another way of checking against null?

andresantacruz
  • 1,676
  • 10
  • 17
  • It looks like is some anonymous type check. But the anonymous type must be defined before. – DrkDeveloper Jul 11 '21 at 13:00
  • Can you share a [mcve]? – mjwills Jul 11 '21 at 13:35
  • @mjwills I really thought that line was enough, but okay. Just updated the question with a minimal reproducible example... – andresantacruz Jul 11 '21 at 14:50
  • 1
    This thing is called [*recursive pattern matching*](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-8.0/patterns) – JL0PD Jul 11 '21 at 15:17
  • @JL0PD thanks! Do you want to submit an answer so I can accept it? – andresantacruz Jul 11 '21 at 15:42
  • See my comments below. It's called the object pattern and it's a not-null check. But pro-tip: stick your code in [SharpLab.io](https://sharplab.io/#v2:CYLg1APgAgTADAWAFCwIzI05UAsACAWQE8CBTAFwAsB7YACgEpkBvZPdwogJVIGNqATsDwBbIgDUAhgLwBePADtSAdzqo4AGkUBXADa6GAbkwc8ASwBmeOmKkyzAZzzNzIZ3gC+npklOtfpoEA9EF4APxsHB4mHJbWttLmTi4AVm4uXh4+fpGBHCHhuZ7I0VhlAnyCwsQ8/EJ0Zgrk5lqN5GF4KUaYQA) and then look at the c# tab to see how it gets translated/rewritten. Also the first subpattern is a no-op since int can't be null. So in your case the first if is just checking if myvar is not null – pinkfloydx33 Jul 11 '21 at 20:27

2 Answers2

4

It's for checking if it's not null in c# 7. It leverages pattern matching. Since c# 9 it's no longer needed as you can use is not null

Miq
  • 3,931
  • 2
  • 18
  • 32
  • 1
    How would you use `is not null` in this case? Isn't this more about testing if the field is null? – huysentruitw Jul 11 '21 at 13:02
  • So the empty double brackets used in pattern matching means `not null`? – andresantacruz Jul 11 '21 at 13:05
  • Yes. It's called the object pattern. And it means whatever you test against is not null. You can now (but couldn't at the time) use `is not null` instead. Though you also have a recursive pattern (the i/j properties) which is easier to use the empty braces – pinkfloydx33 Jul 11 '21 at 20:24
-1

It's part of pattern matching.

https://learn.microsoft.com/dotnet/csharp/language-reference/operators/patterns#property-pattern

What you wrote should pass regardless as long as it is a value, similar to writing

if (myVar is object && myVar.i is object)
{
}

if (myVar is object && myVar.j is object)
{
}
Andrei15193
  • 655
  • 5
  • 8
  • Thanks for the reference link. However it seems to be very different from what you suggested. What I wrote is checking against null, not if it's object; or did I understand wrong? – andresantacruz Jul 11 '21 at 15:51
  • 1
    @andresantacruz actually checking for object equates to checking for not null, `value != null` is equivalent to `!(value is null)` and `value is object`. – Andrei15193 Jul 11 '21 at 21:00
  • 1
    Not quite the same: when `myVar` is `null`, the code in this answer will throw a `NullReferenceException` while the code in the question would not. – huysentruitw Jul 15 '21 at 19:18
  • @huysentruitw that is correct, thanks for pointing it out. I have updated the answer. – Andrei15193 Jul 16 '21 at 07:59
  • rowVersion is not null && rowVersion.Length < 8 (same as pattern) rowVersion is { Length: < 8 } – alhpe Jan 25 '22 at 11:25