Friday, June 14, 2013

How to Compare Type in C#

How to CompareType

using System;
class A { }
class B : A { }
class UseIs
{
static void Main()
{
A a = new A();
B b = new B();
if (a is A) Console.WriteLine("a is an A");
if (b is A)
Console.WriteLine("b is an A because it is derived from A");
if (a is B)
Console.WriteLine("This won’t display -- a not derived from B");
if (b is B) Console.WriteLine("b is a B");
if (a is object) Console.WriteLine("a is an object");
}

}

The output is shown here:
a is an A
b is an A because it is derived from A
b is a B
a is an object

No comments:

Post a Comment

Your comment is pending for approval

AngularJS Basics - Part 1

                                                                  AngularJS What is AngularJS ·          Framework by googl...