Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All
Expand Minimize

Type.ToString Method

Returns a String representing the name of the current Type.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

public override string ToString()

Return Value

Type: System.String
A String representing the name of the current Type.

Implements

_Type.ToString()

This method returns the fully qualified common language runtime namespace and name for all primitive types. For example, the C# instruction, (long)0.Type().ToString() returns "System.Int64" instead of merely "Int64".

If the current Type represents a generic type, the type and its type arguments are qualified by namespace and by nested type, but not by assembly. If the current Type represents a type parameter in the definition of a generic type or generic method, this method returns the unqualified name of the type parameter.

This following example demonstrates a use of the Namespace and Module properties and the ToString method of Type.

using System;

namespace MyNamespace
{
    class MyClass
    {
    }
}

public class Example
{
    public static void Main()
    {
         Type myType = typeof(MyNamespace.MyClass);
         Console.WriteLine("Displaying information about {0}:", myType); 
         // Get the namespace of the myClass class.
         Console.WriteLine("   Namespace: {0}.", myType.Namespace);
         // Get the name of the module.
         Console.WriteLine("   Module: {0}.", myType.Module);
         // Get the fully qualified type name.
         Console.WriteLine("   Fully qualified name: {0}.", myType.ToString());
    }
}
// The example displays the following output: 
//    Displaying information about MyNamespace.MyClass: 
//       Namespace: MyNamespace. 
//       Module: type_tostring.exe. 
//       Fully qualified name: MyNamespace.MyClass.

The following example compares the strings returned by the ToString method and the Name, FullName, and AssemblyQualifiedName properties.

using System;
using System.Collections.Generic;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      Type t = typeof(String);
      ShowTypeInfo(t);

      t = typeof(System.Collections.Generic.List<>);
      ShowTypeInfo(t);

      var list = new List<String>();
      t = list.GetType();
      ShowTypeInfo(t);

      Object v = 12;
      t = v.GetType();
      ShowTypeInfo(t);

      t = typeof(IFormatProvider);
      ShowTypeInfo(t);

      IFormatProvider ifmt = NumberFormatInfo.CurrentInfo;
      t = ifmt.GetType();
      ShowTypeInfo(t);
   }

   private static void ShowTypeInfo(Type t)
   {
      Console.WriteLine("Name: {0}", t.Name);
      Console.WriteLine("Full Name: {0}", t.FullName);
      Console.WriteLine("ToString:  {0}", t.ToString());
      Console.WriteLine("Assembly Qualified Name: {0}",
                        t.AssemblyQualifiedName);
      Console.WriteLine();
   }
}
// The example displays output like the following: 
//    Name: String 
//    Full Name: System.String 
//    ToString:  System.String 
//    Assembly Qualified Name: System.String, mscorlib, Version=4.0.0.0, Culture=neutr 
//    al, PublicKeyToken=b77a5c561934e089 
// 
//    Name: List`1 
//    Full Name: System.Collections.Generic.List`1 
//    ToString:  System.Collections.Generic.List`1[T] 
//    Assembly Qualified Name: System.Collections.Generic.List`1, mscorlib, Version=4. 
//    0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 
// 
//    Name: List`1 
//    Full Name: System.Collections.Generic.List`1[[System.String, mscorlib, Version=4 
//    .0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] 
//    ToString:  System.Collections.Generic.List`1[System.String] 
//    Assembly Qualified Name: System.Collections.Generic.List`1[[System.String, mscor 
//    lib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], mscorl 
//    ib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 
// 
//    Name: Int32 
//    Full Name: System.Int32 
//    ToString:  System.Int32 
//    Assembly Qualified Name: System.Int32, mscorlib, Version=4.0.0.0, Culture=neutra 
//    l, PublicKeyToken=b77a5c561934e089 
// 
//    Name: IFormatProvider 
//    Full Name: System.IFormatProvider 
//    ToString:  System.IFormatProvider 
//    Assembly Qualified Name: System.IFormatProvider, mscorlib, Version=4.0.0.0, Cult 
//    ure=neutral, PublicKeyToken=b77a5c561934e089 
// 
//    Name: NumberFormatInfo 
//    Full Name: System.Globalization.NumberFormatInfo 
//    ToString:  System.Globalization.NumberFormatInfo 
//    Assembly Qualified Name: System.Globalization.NumberFormatInfo, mscorlib, Versio 
//    n=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0, 2.0, 1.0

Portable Class Library

Supported in: Portable Class Library

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8
Show:
© 2015 Microsoft