Runtime Type Identification in C#
Here we discuss about a feature of C# called RTID (Runtime Type Identification).
With the help of this, we can identify a type of an object during the execution
of the program. And we can easily get that the casting is possible or not.
There are three keywords, which supports
Runtime Type Identification: is, as and type of.
Now we will be discussing about this:Using is: The syntax of the is keyword is :
expr is Type
Here expr is the expression, which type is tested against the Type.
For ex: In this example we create three classes (first, second, third), here second and third derived from the first class, in this program we will check that the following classes will be derived from the first class:

After that, we create a main class TryIs:
class TryIs
{
public static void main()
{
first f = new first();
second s = new second();
third t = new third();
if (s is first)
{
Console.WriteLine("It is in the first class");
}
if (t is second)
{
Console.WriteLine("It is in the second class");
}
if (t is first)
{
Console.WriteLine("It is in first class");
}
}
}

Prabakaran MPosted Oct 19, 2019, 2:32 AM
Nice blog thanks for sharing
RumaPosted Sep 3, 2012, 7:04 AM
Program1: The output of the first program will be : It is in the first class It is in first class Since second and third both the classes are derived from first. if(t is Second) will give the warning: The given expression is never of the provided ('Second') type. Program 2: Instead of writing s=f as s; we have to write s= f as second; else it will give the arror "The type or namespace name 's' colud not be found'. It's not possible to typecast s = f as second but opposite direction it's allowed. So if we write f= s as first. It will give the output "type casting is allowed"