Type.ToString Method

July 28, 2014

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.

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 Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      try
      {
         Type myType = typeof(MyNamespace.MyClass);
         outputBlock.Text += String.Format("\nPrinting the details of {0}.\n", myType) + "\n";
         // Get the namespace of the class Example.
         outputBlock.Text += String.Format("Namespace: {0}.", myType.Namespace) + "\n";
         // Get the name of the module.
         outputBlock.Text += String.Format("Module: {0}.", myType.Module) + "\n";
         // Get the fully qualified common language runtime namespace.
         outputBlock.Text += String.Format("Fully qualified type: {0}.", myType.ToString()) + "\n";
      }
      catch (Exception e)
      {
         outputBlock.Text += "Exception: " + e.Message + "\n";
      }
   }
}


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2014 Microsoft