Demonstrate the 'is' operator - CSharp Custom Type

CSharp examples for Custom Type:is

Description

Demonstrate the 'is' operator

Demo Code

using System;/*w  w w  . j av  a 2 s. c o  m*/
class Dog {
}
class Cat {
}
public class IsDemo {
    public static void Main( ) {
        Dog myDog = new Dog( );
        Cat myCat = new Cat( );
        int i = 10;
        WhatIsIt(myDog);
        WhatIsIt(myCat);
        WhatIsIt(i);
    }
    public static void WhatIsIt( object o ) {
        if( o is Dog )
            Console.WriteLine("It is a dog");
        else if( o is Cat )
            Console.WriteLine("It is a cat");
        else
            Console.WriteLine("I don't know what it is");
    }
}

Result


Related Tutorials