IS Keyword:
Is keyword is useful when we want to check if the variables are the same type or not. Please check the below snapshot for output.
- class Program
- {
- static void Main(string[] args)
- {
- object str1 = "Madan";
- if(str1 is string)
- {
- Console.WriteLine("yes it is a number");
- Console.ReadLine();
- }
- }
- }
AS keyword:
str1 is an object and we are converting an object to a string. As keyword is helpful when we want to convert an object from one type to another type.
Key points to remember.
IS keyword is helpful to check if the object is that type or not
AS keyword is helpful too if we want to convert one type to another type.
IS keyword type is boolean and AS keyword is not a boolean type.
The is operator returns true if the given object is of the same type whereas as operator returns the object when they are compatible with the given type.