Activator::CreateInstance Method (Type)

Creates an instance of the specified type using that type's default constructor.

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

static Object^ CreateInstance(
	Type^ type


Type: System::Type

The type of object to create.

Return Value

Type: System::Object
A reference to the newly created object.


type is nullptr.


type is not a RuntimeType.


type is an open generic type (that is, the ContainsGenericParameters property returns true).


type cannot be a TypeBuilder.


Creation of TypedReference, ArgIterator, Void, and RuntimeArgumentHandle types, or arrays of those types, is not supported.


The assembly that contains type is a dynamic assembly that was created with AssemblyBuilderAccess::Save.


The constructor being called throws an exception.


In the .NET for Windows Store apps or the Portable Class Library, catch the base class exception, MemberAccessException, instead.

The caller does not have permission to call this constructor.


Cannot create an instance of an abstract class, or this member was invoked with a late-binding mechanism.


The COM type was not obtained through GetTypeFromProgID or GetTypeFromCLSID.


In the .NET for Windows Store apps or the Portable Class Library, catch the base class exception, MissingMemberException, instead.

No matching public constructor was found.


type is a COM object but the class identifier used to obtain the type is invalid, or the identified class is not registered.


type is not a valid type.

The constructor to be invoked must be accessible.


Starting with the .NET Framework 2.0 Service Pack 1, this method can be used to access nonpublic types if the caller has been granted ReflectionPermission with the ReflectionPermissionFlag::RestrictedMemberAccess flag and if the grant set of the assembly that contains the nonpublic types is restricted to the caller’s grant set or to a subset thereof. (See Security Considerations for Reflection.)

To use this functionality, your application should target the .NET Framework 3.5 or later.

The following code example demonstrates how to call the CreateInstance(Type) method. Instances of several different types are created and their default values are displayed.

using namespace System;

ref class DynamicInstanceList
    static String^ instanceSpec = "System.EventArgs;System.Random;" +

    static void Main()
        array<String^>^ instances = instanceSpec->Split(';');
        Array^ instlist = Array::CreateInstance(Object::typeid, instances->Length);
        Object^ item;

        for (int i = 0; i < instances->Length; i++)
            // create the object from the specification string
            Console::WriteLine("Creating instance of: {0}", instances[i]);
            item = Activator::CreateInstance(Type::GetType(instances[i]));
            instlist->SetValue(item, i);
        Console::WriteLine("\nObjects and their default values:\n");
        for each (Object^ o in instlist)
            Console::WriteLine("Type:     {0}\nValue:    {1}\nHashCode: {2}\n",
                o->GetType()->FullName, o->ToString(), o->GetHashCode());

int main()

// This program will display output similar to the following: 
// Creating instance of: System.EventArgs 
// Creating instance of: System.Random 
// Creating instance of: System.Exception 
// Creating instance of: System.Object 
// Creating instance of: System.Version 
// Objects and their default values: 
// Type:     System.EventArgs 
// Value:    System.EventArgs 
// HashCode: 46104728 
// Type:     System.Random 
// Value:    System.Random 
// HashCode: 12289376 
// Type:     System.Exception 
// Value:    System.Exception: Exception of type 'System.Exception' was thrown. 
// HashCode: 55530882 
// Type:     System.Object 
// Value:    System.Object 
// HashCode: 30015890 
// Type:     System.Version 
// Value:    0.0 
// HashCode: 1048575

.NET Framework

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

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
© 2014 Microsoft