C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




Runtime Type Identification ( R T T I )


Posted Date: 06 Mar 2004    Resource Type: Articles    Category: .NET Framework
Author: ANITA MARY JOSEPHMember Level: Gold    
Rating: Points: 7



You can determine whether an object is of a certain type by using the ' is ' operator.
Sometimes you will want to try a cast at runtime but not raise an exception if the cast fails.To do this use the ' as ' operator. Often, you will need to obtain information about a type. To do this, C# supplies the ' typeof ' operator.

using System;
using System.IO;
class First {}
class Second : First {}
class C : First {}
class RTTIDemo
{

public static void Main()
{
First a = new First();
Second b = new Second();
if( a is First )
Console.WriteLine ( "a is a First" );
if( b is First)
Console.WriteLine( "b is a First because it is deriived from First" );
if(b is Second)
Console.WriteLine( "b is Second" );
if(a is object)
Console.WriteLine("a is an Object");

C c = new C();
c = a as C;

if(c == null)
Console.WriteLine( "Cast c = (C) a is not allowed." );
else
Console.WriteLine( "Cast c = (C)a is allowed" );

Type t = typeof( StreamReader );
Console.WriteLine( t.FullName );

if ( t.IsClass )
Console.WriteLine( "Is a class" );
if( t.IsAbstract )
Console.WriteLine( "Is abstract." );
else
Console.WriteLine( "Is concrete." );

Console.Read();
}
}





Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Array initializers
Previous Resource: Making the datagrid non editable
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design


Contact Us    Privacy Policy    Terms Of Use