Export (0) Print
Expand All

Type.ToString Method

Returns a String representing the name of the current Type.

[Visual Basic]
Overrides Public Function ToString() As String
[C#]
public override string ToString();
[C++]
public: String* ToString();
[JScript]
public override function ToString() : String;

Return Value

A String representing the name of the current Type.

Remarks

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".

Example

[Visual Basic, C#, C++] This following example demonstrates a use of the Namespace and Module properties and the ToString method of Type.

[Visual Basic] 
Imports System
Imports Microsoft.VisualBasic
Namespace MyNamespace
    Class [MyClass]
    End Class '[MyClass]
End Namespace 'MyNamespace
Public Class Type_ToString_3
    Public Shared Sub Main()
        Try
            Dim myType As Type = GetType(MyNamespace.MyClass)
            Console.WriteLine(ControlChars.NewLine + "Printing the details of {0}." + ControlChars.NewLine, myType)
            ' Get the namespace of the class Type_ToString_3.
            Console.WriteLine("Namespace: {0}.", myType.Namespace)
            ' Get the name of the module.
            Console.WriteLine("Module: {0}.", myType.Module)
            ' Get the fully qualified common language runtime namespace.
            Console.WriteLine("Fully qualified type: {0}.", myType.ToString())
        Catch e As Exception
            Console.WriteLine("Exception: " + e.Message.ToString())
        End Try
    End Sub 'Main
End Class 'Type_ToString_3

[C#] 
using System;
namespace MyNamespace
{
    class MyClass
    {
    }
}
public class Type_ToString_3
{
    public static void Main()
    {
        try
        {
            Type myType = typeof(MyNamespace.MyClass);
            Console.WriteLine("\nPrinting the details of {0}.\n", myType); 
            // Get the namespace of the class Type_ToString_3.
            Console.WriteLine("Namespace: {0}.", myType.Namespace);
            // Get the name of the module.
            Console.WriteLine("Module: {0}.", myType.Module);
            // Get the fully qualified common language runtime namespace.
            Console.WriteLine("Fully qualified type: {0}.", myType.ToString());
        }
        catch(Exception e)
        {
            Console.WriteLine("Exception: " + e.Message ); 
        }
    }
}

[C++] 
#using <mscorlib.dll>

using namespace System;

namespace MyNamespace {
   __gc class MyClass {
   };
}

int main() {
   try {
      Type*  myType = __typeof(MyNamespace::MyClass);
      Console::WriteLine(S"\nPrinting the details of {0}.\n", myType);
      // Get the namespace of the class MyClass.
      Console::WriteLine(S"Namespace: {0}.", myType->Namespace);
      // Get the name of the module.
      Console::WriteLine(S"Module: {0}.", myType->Module);
      // Get the fully qualified common language runtime namespace.
      Console::WriteLine(S"Fully qualified type: {0}.", myType);
   } catch (Exception* e) {
      Console::WriteLine(S"Exception: {0}", e->Message);
   }
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

Type Class | Type Members | System Namespace | String

Show:
© 2014 Microsoft