.Net using the IS operator

using System;

namespace CsharpISOperator
{
    internal class TestClassOne { }
    internal class TestClassTwo { }
    internal class TestClassThree : TestClassOne { }

    public class Program
    {
        static void TestIsOperator(object inputObject)
        {
            TestClassOne testClassOne;
            TestClassTwo testClassTwo;

            if (!(inputObject is TestClassOne))
            {
                if (inputObject is TestClassTwo)
                {
                    Console.WriteLine("inputObject is Class2");
                    testClassTwo = (TestClassTwo)inputObject;
                }
                else
                {
                    Console.WriteLine("inputObject is neither Class1 nor Class2.");
                }
            }
            else
            {
                Console.WriteLine("inputObject is Class1");
                testClassOne = (TestClassOne)inputObject;
            }

            Console.ReadKey();
        }

        static void Main()
        {
            var classOne = new TestClassOne();
            var testClassTwo = new TestClassTwo();
            var testClassThree = new TestClassThree();

            TestIsOperator(classOne);
            TestIsOperator(testClassTwo);
            TestIsOperator(testClassThree);
        }
    }
}

Leave a Reply

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