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.