What is IS and AS operator in c# .NET?
Is Operator is used to Check the Compatibility of an Object with a given Type and it returns the result as a Boolean.
if(obj is int){ ...}
if(obj is int)
{
...
}
As Operator is used for Casting of Object to a given Type or a Class. The as keyword is used to cast nullable types if the specified value is not an instance of the specified type.
AnyClass anyObject = obj as AnyClass;
sometimes in a program we havily use polymorphism and we do not know the actual type of the object , so here for comparison we use IS operator just to check whether the object is of a particular type or not.As is simply used to convert an object into specific type if the conversion is possible ,where object must be of that type or a child of that type.The real reason of existance of these two are shorting out polymorphism problems.
AS Typecasting And Handling Object IS Just Like Exist
The “IS” operator is used to check if the run-time type of an object is compatible with the given type or not, whereas the “AS” operator is used to perform conversion between compatible reference types or nullable types