2

So, I think in the current C# version which is C# 8. There are a few ways to check the reference types are not null. I am confused by these two below:

  1. o is object
  2. o is {}

What's the main difference between them, I cannot find much info for them.

Can someone explain it to me? Which one is recommended? Or what are the pro and cons of each one?

Kirk Larkin
  • 84,915
  • 16
  • 214
  • 203
Andy Song
  • 4,404
  • 1
  • 11
  • 29
  • 1
    don't forget `o != null` – Greg Nov 02 '20 at 21:38
  • @Greg I understand what that means, I listed that I do not understand. – Andy Song Nov 02 '20 at 21:39
  • Does this answer your question https://stackoverflow.com/q/6417902/1260204 – Igor Nov 02 '20 at 21:39
  • @Igor no it does not. – Andy Song Nov 02 '20 at 21:40
  • 1
    It used to be that `is` checked only if the left hand operand was of the same type as the right hand operand, or one of the descendant types, it also ensured the left hand operand was non-null. `is` has changed meaning now, and is also used for pattern matching, and the second expression is the pattern matching way of saying "an object" (as in, not null). So the two expressions you have there does the exact same thing, and will even compile into the exact same code. – Lasse V. Karlsen Nov 02 '20 at 21:41
  • 3
    Note that `x is type` will only check that `x` is of the same type as `type`, or one of its descendant types, whereas `x is pattern` is much more powerful when it comes to property matching, such as `shape is Rectangle (100, _)` which matches any `Rectangle` object, for which there is a `Deconstruct` method with two output parameters, first of which will match the value 100. – Lasse V. Karlsen Nov 02 '20 at 21:44
  • 1
    Actually, I'm pretty sure this is a duplicate so I'd rather avoid doing that until I'm convinced it's not. – Lasse V. Karlsen Nov 02 '20 at 21:46
  • @LasseV.Karlsen so the first one is using the `is type` checking, and the second one is using pattern matching. and `{}` is just a way of saying this is an object in pattern matching. when it comes to il code they should have the same code. Do I understand it correctly? – Andy Song Nov 02 '20 at 21:53
  • @LasseV.Karlsen: it's definitely a duplicate...I'm starting my search now, but I saw and closed essentially this exact same question just in the last month or two. (If only there was an easy way to do a search on closed and/or deleted question by user-casting-vote). I don't have a lot of patience these days to look for dupes, so I might come up empty...but I assure you this question is here with an answer already somewhere, for the person who takes the time to look. – Peter Duniho Nov 02 '20 at 22:03
  • See also https://stackoverflow.com/questions/62139886/c-sharp-meaning-of-curly-braces-after-the-is-operator and https://stackoverflow.com/questions/62212715/what-does-is-syntax-mean-in-c – Peter Duniho Nov 02 '20 at 22:07
  • _"when it comes to il code they should have the same code"_ -- that's easy enough for you to verify yourself, if it matters to you. – Peter Duniho Nov 02 '20 at 22:07

1 Answers1

2
  1. o is object is the original way of checking the type of an instance (available since C# 1.0)

  2. o is {} uses the pattern matching with property patterns introduced in C# 8.0

    {} denotes an object instance with any (or none) properties, i.e. any object instance.

Ivan Neeson
  • 2,823
  • 2
  • 16
  • 11
adjan
  • 13,371
  • 2
  • 31
  • 48