4

I'm starting to see these statements and I am trying to wrap my head around these kind of statements.

if (obj is SomeAuto car)
{
   //Do stuff
}

If I understand correctly we basically are casting obj into the variable car which would be a type "SomeAuto"?

1) What is the official terminology of this statement?

2) What would happen if I wanted to change the if statement to conditionally execute for a particular reason?

For example say SomeAuto is a base class and I only wanted a certain type of auto, or say I want all of the SomeAuto except maybe one particular kind.

NineBerry
  • 26,306
  • 3
  • 62
  • 93
John Doe
  • 3,053
  • 17
  • 48
  • 75
  • See https://stackoverflow.com/questions/184681/is-vs-typeof – Jay Buckman Oct 13 '17 at 14:29
  • 3
    Looks like the C#7 type pattern matching: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/is#type – UnholySheep Oct 13 '17 at 14:30
  • UnholySheep answered 1) , for 2) you would probably need another pattern. – Fildor Oct 13 '17 at 14:31
  • 1
    Also, 2 questions should be 2 questions. You could edit and add a disclaimer that you don' know what the terminology is, and stick to just the question about adding extra conditions – musefan Oct 13 '17 at 14:36
  • thanks unholysheep – John Doe Oct 13 '17 at 14:37
  • _"For example say SomeAuto is a base class and I only wanted a certain type of auto, or say I want all of the SomeAuto except maybe one particular kind."_ Visitor Pattern comes to my mind. – Fildor Oct 13 '17 at 14:38
  • 2
    @PrivateJoker - Please give a specific case you are refering to for the second question as in its current state it is making the question too broad. (Even better would be to remove that part into a different question – Gilad Green Oct 13 '17 at 14:39
  • Yes. Definitely move it to a different question. Stack overflow policy is one question per question. If you have multiple questions ask the on multiple posts. – Chris Oct 13 '17 at 15:04

1 Answers1

6

This if statement is using the is expression added in C# 7.0 under pattern matching. Docs specify that:

The is pattern expression extends the familiar is operator to query an object beyond its type.

It enables you to check if obj is of a specific type and also assigns the casted result into a variable.


Before these feature you'd probably write:

var car = obj as SomeAuto;
if(car != null)
{
    //Do Stuff
}

As pointed out by @BurnBA a difference when using the as than the original is is that Note that the as operator performs only reference conversions, nullable conversions, and boxing conversion and therefore cannot be used to check non-nullable value types.

IS4
  • 11,945
  • 2
  • 47
  • 86
Gilad Green
  • 36,708
  • 7
  • 61
  • 95
  • eh, minor caveat, `as` only works with reference types, while you can use `is` for things like `int`s. – BurnsBA Oct 13 '17 at 14:35
  • @BurnsBA - Good comment. If you could bring a reference to that and I'll update my answer accordingly – Gilad Green Oct 13 '17 at 14:36
  • 2
    "Note that the as operator performs only reference conversions, nullable conversions, and boxing conversions. The as operator can't perform other conversions, such as user-defined conversions, which should instead be performed by using cast expressions." https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/as – BurnsBA Oct 13 '17 at 14:39
  • If I remember correctly, this is used only for reference types. For value types, the plain old `is`+cast is performed. – IS4 Oct 13 '17 at 14:42
  • Also, the online doc refers to the language spec, see 7.10.10 (is) and 7.10.11 (as) at least for version 5.0 in front of me. – BurnsBA Oct 13 '17 at 14:44
  • @BurnsBA - the `is` operator is from a prior C# version, 5 if I'm not mistaken, but the `is expression` is only from later – Gilad Green Oct 13 '17 at 14:46