IS vs AS Keyword ( C# Interview questions with answers)

IS vs AS Keyword ( C# Interview questions with answers)


In this video will try to understand what is the use of IS keyword and AS keyword in C# In order to make you understand the difference between the IS keyword and the AS keyword Le me paste two sentences before you If you read both of these sentences you will come to know what is the use of the IS keyword and what is the use of the AS keyword Here are both the sentences I have pasted two sentences here If you read both of these sentences You will come to know the difference between the IS keyword and the AS keyword If you read the first sentence carefully IS is str1 variable type of string The IS keyword is useful when you want to check if two objects are of the same type or not Look at the second sentence Can we convert str1 string as a object The AS keyword is useful when you want to typecast object from one type to the other type First demonstrate the IS keyword object str1=Shiv if str1 is string then do something This is of same type If I run this program so let me put a debug point here There my application is running so you can see that it goes here Because str1 is of type string this is true and it goes inside this if condition I do something like this let say 1234 If I run this The str1 is of type number He does not go inside this if condition why? because the str1 now of a type number The IS keyword is useful when you want to check if the variables are of the same type or not str1 is a string The AS keyword helps you to convert a object from one type to the other type This str1 is a object type Convert this str1 as a string This str1 is a object Because of this AS keyword he will try to convert this str1 into a string If I run this program here so If I just click on start here The str1 is a object my naming convention is bad here, I have kept the name is str but this str1 is actually a object The x has the value shiv The AS keyword is helpful when you want to convert a object from one type to the other type In case if the AS keyword is not able to convert the object from one type to the other type then the value is null because this is a number probably he will not be able to convert it into the string The x will be null so if I run this This str1 is actually a number,a pure number it is not a string This x will have null values The AS keyword is not able to convert the type from one object to the other type He will actually return null That was a very simple demonstration if of the IS keyword and the AS keyword Next time if you are getting confused just remember these two sentences The IS keyword is helpful to check if the object is of that type or not The AS keyword is helpful if you want to convert the objects from one type to the other type. Thank you so much

10 thoughts on “IS vs AS Keyword ( C# Interview questions with answers)

  • As keyword can not type cast to not nullable datatypes.
    object obj1 = 1234;
    int num1 = obj1 as int; // compile time error "The as operator must be used with a reference type or nullable types"

  • can you Explain :
    ClassA{
    }
    ClassB{
    public static void Main(String args[])
    {
    ClassA objA=new ClassA();
    ClassB objB=objA as ClassB;
    }
    }

    is it run successfully or throw compile error and why ? Error? Or will it copy all references in object ClassB?

  • When i write object [] arrdata = dict["data"] as object []; It returns me null.. But it returned values in some other code I've written.. Don't know the reason.

  • The is and as operators in C# verify whether the specified interface is implemented or not. The is

    operator is used to check the compatibility between two types or classes. It returns a boolean value

    based on the check operation performed. On the other hand, the as operator returns null if the two

    types or classes are not compatible with each other.

Leave a Reply

Leave a Reply

Your email address will not be published. Required fields are marked *