C# FAQ: How get the type name at runtime

Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio


Jump to: navigation, search
CSharp-Online.NET:FAQs
edit

How get the type name at runtime?

To get the name of a C# type at runtime, use the GetType() method of object class. For instance, the following source code:

using System; 
 
class Example
{
   class ClassAppl 
   {    
      static void ShowTypeInfo (object o) 
      { 
      Console.WriteLine ("type name = {0}, 
         full type name = {1}", o.GetType(), 
         o.GetType().FullName ); 
      }
 
      public static void Main()
      { 
         long longType = 99; 
         Example example= new Example(); 
 
         ShowTypeInfo (example); 
         ShowTypeInfo (longType); 
      }
   }
}

generates the following output:


 Example output (program output)
type name = Example, full type name = Example

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

All types inherit from object; so, all types can access GetType().


Personal tools