3

When considering the is versus as in C#, you can use either to confirm if a type is convertible to another type.

// using is
Employee e = new Manager();
if (e is Manager) { 
    var m = (Manager) e; 
    // m is now type `Manager`
}

// using as
Employee e = new Manager(); 
Manager m = e as Manager; 
// m is now type `Manager`
if (m != null) { 

}

I understand how both operators work and how to use them. Consider the is operator checks the type twice while as checks once, and they both have the same restrictions regarding what types of conversions they support, is there ever a compelling reason to use is?

The marked duplicate is asking what is the difference between the two operators. My question is specifically asking "Understanding what both do, why use is?" They are not the same question, nor do they have the same answer.

jdphenix
  • 15,022
  • 3
  • 41
  • 74
  • 1
    have you not checked out the msdn site on how to safely cast by using IS and AS https://msdn.microsoft.com/en-us/library/cc488006.aspx `the as operator is more efficient because it actually returns the cast value if the cast can be made successfully. The is operator returns only a Boolean value. It can therefore be used when you just want to determine an object's type but do not have to actually cast it.` – MethodMan Jun 23 '15 at 21:19
  • I understand how to use both operators to safely cast types. I'm more questioning if there's ever a compelling reason to use `is` rather than `as`. – jdphenix Jun 23 '15 at 21:19
  • Duplicate: http://stackoverflow.com/a/3786390/4705221 – Patrick Murphy Jun 23 '15 at 21:19
  • 1
    I don't think `is` casts the type, it only checks it. `as` casts it. – Kevin Seifert Jun 23 '15 at 21:20
  • I also understand what both operators do. I am asking that if `is` checks the type twice versus once for `as`, is there ever a reason to use `is`. – jdphenix Jun 23 '15 at 21:21
  • I think to make a long explanation short.. the `is` is assuming where the `as` is actually doing a valid cast – MethodMan Jun 23 '15 at 21:22
  • Use is when you don't want to cast but want to know if it is compatible. Use as when you want to cast a nullable type – Patrick Murphy Jun 23 '15 at 21:22
  • @PatrickMurphy not exactly duplicate, but maybe worth adding that difference - I'll make comment on that post. – Alexei Levenkov Jun 23 '15 at 21:31

2 Answers2

12

You must use is instead of as when the destination type is a non-nullable value type:

object obj = 0;
int i = obj as int; // compilation error because int can't represent null

if (obj is int)
{
    int j = (int)obj; // works
}
Michael Liu
  • 52,147
  • 13
  • 117
  • 150
3

The is operator performs a type-check. The as operator performs a type-check and (if possible) a cast.

is there ever a compelling reason to use is?

I can think of a couple scenarios. First, as this answer already points out, you can't as-cast non-nullable value types.

But your original example, although extremely common, is heavily biased towards as. It all depends what you are going to do after your cast and/or type-check.

Let's say you will be calling the following method if your cast succeeds:

private void PerformManagerDuty(Manager m) 
{
    //Stuff happens
}

Performing the as cast then the null check takes an extra line of code than is:

//as casting with null check
var m = e as Manager; 
if (m != null)
{
    PerformManagerDuty(m);
}

//is check before cast
if (e is Manager)
{ 
    PerformManagerDuty((Manager)e);
}

Also, if you prefer, after performing a type-check with is, you can perform an as cast with no performance loss:

if (e is Manager)
{ 
    PerformManagerDuty(e as Manager);
}
Community
  • 1
  • 1
User
  • 1,118
  • 7
  • 23