Vinod Salunke
What is IS and AS operator in c# .NET?

What is IS and AS operator in c# .NET?

By Vinod Salunke in C# on Aug 08 2020
  • Cristopher Coronado
    Aug, 2020 11

    Is Operator is used to Check the Compatibility of an Object with a given Type and it returns the result as a Boolean.

    1. if(obj is int)
    2. {
    3. ...
    4. }

    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.

    1. AnyClass anyObject = obj as AnyClass;

    • 3
  • Sachin Singh
    Oct, 2020 20

    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.

    • 1
  • Mehran
    Nov, 2024 17

    AS Typecasting And Handling Object IS Just Like Exist

    • 0
  • Praveen Padle
    Aug, 2020 11

    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

    • 0


Most Popular Job Functions


MOST LIKED QUESTIONS