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

Posted By on May 5, 2019



in this video we'll try to understand what is the use of is keyword and as keyword in she shop now in order to make you understand the difference between the East keyword and the as keyword let me go and paste two sentences before you and if you read both of these sentences you know you will come to know what is the use of the East keyword and what is the use of the as keyword so here are both the sentences so let me just do a control V and you can see that I have pasted two sentences here and if you read both of these sentences you know you will come to know the difference between the East keyword and the as keyword so if you read the first sentence carefully it says that is str1 variable type of string so the is keyword is useful you know when you want to check if two objects are of the same type or not look at the second sentence can we convert STR one string as a object so the as keyword is useful when you want to typecast object from one type to the other type so let's first demonstrate the East keyword so for example now let's say I have something like this object X is equal to R LSA object STR 1 is equal to shave okay so I can say here if I STR 1 is string then do something and I'll just put a console dot write line you're saying this is of same type okay so now if I go and run this program so let me put a debug point here so if I go and run this so that my application is running so you can see that it goes here and because STR 1 is of type string right you can see this this is true and it goes inside this if condition right in case I go and do something like this in case I say that this is let's say 1 2 3 4 like this ok and if I go and run this now the STR 1 is of type number right so if I go and say if here you can see he does not go inside this if condition why because the str1 is now of a type number right so the East keyword is useful you know when you want to check if the variables are of the same type or not now for the demonstration of the as keyword so for example now let's say you know this STR one is a string right so the as keyword helps you to convert a object from one type to the other type so for example now this is actually a string right but this STR one is an object type so let's say I can do something like this I can say a string X is equal to convert this STR one as a string so this STR one is an object right but because of this as keyword he will try to convert this STR one into a string so if I go and run this program here so if I just click on start here remember the STR one is object you know because my naming convention is bad here I've kept the name as STR but this STR one is actually object okay so there it goes and if I say do this conversion right you can see now the X has the value Shiv right so the as keyword is helpful you know anyone to convert a object from one type to the other type and 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 so for example now let's say if this was a number okay so because this is a number you know probably he will not be able to convert it into the string right so the X will be null so if I go and run this if you take this condition at this moment so if you see here now this STR one is actually a number right a pure number it is not a string so what will happen is this X will have null values so in case the as keyword is not able to convert the type from one object to the other type right he will actually return null right so that was a very simple demonstration of the East keyword and as keyword so next time you know if you are getting confused just remember these two sentences the East keyword is helpful to check if the object is of that type or not and the ASCII word is help is helpful you know if you want to convert the objects from one type to the other type thank you so much you

Posted by Lewis Heart

This article has 9 comments

  1. 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.

    Reply
  2. 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.

    Reply
  3. 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?

    Reply
  4. 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"

    Reply
  5. IS vs AS keyword of C# explained ( C# interview question with answers)
    Thanks to www.questpond.com to create this video

    Reply

Leave a Reply

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