Type operators: Is : is « Operator « C# / CSharp Tutorial






Testing a Type with "is".

You can determine whether an object is of a certain type by using the "is" operator.

Its general form is shown here:

expr is type
using System;

interface Printable
{
    void print(string name);
}
class Paper: Printable
{
    public void print(string name)
    {
        Console.WriteLine("Poking {0}", name);
    }
}
class NonPrintablePaper
{
}
class MainClass
{
    public static void Test(string sister, params object[] papers)
    {
        foreach (object o in papers)
        {
            if (o is Printable)
            {
                Printable p = (Printable) o;
                p.print(sister);
            }
        }
    }
    public static void Main()
    {
        Test("Test", new Paper(), new NonPrintablePaper());
    }
}
Poking Test








3.12.is
3.12.1.Type operators: Is
3.12.2.is operator in class hiearchy
3.12.3.Use 'is' to avoid an invalid cast.
3.12.4.Working with Interfaces: is operator
3.12.5.Test if someObject is, or is derived from, a TextReader using the is operator
3.12.6.'is' operator for value data type: int, long and float
3.12.7.Use is in Console.WriteLine