Logo 
Search:

C# FAQ

Submit Interview FAQ
Home » Interview FAQ » C#RSS Feeds

Can I get the name of a type at runtime?

  Shared By: Bonifaco Garcia    Date: Nov 18    Category: C#    Views: 761

Answer:

Yes, use the GetType method of the object class (which all types inherit from). For example:

using System;

class CTest
{
class CApp
{
public static void Main()
{
long i = 10;
CTest ctest = new CTest();

DisplayTypeInfo( ctest );
DisplayTypeInfo( i );
}

static void DisplayTypeInfo( object obj )
{
Console.WriteLine( "Type name = {0}, full type name = {1}", obj.GetType(), obj.GetType().FullName );
}
}
}
produces the following output:

Type name = CTest, full type name = CTest
Type name = Int64, full type name = System.Int64

Share: 
 

Didn't find what you were looking for? Find more on Can I get the name of a type at runtime? Or get search suggestion and latest updates.


Your Comment
  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].


Tagged: