Activator.CreateInstance Vs new

This overload of the “Activator.CreateInstance” method is used by compilers to implement the instantiation of types specified by type parameters using generics.

Say you have the following method:

public static T Factory<T>() where T: new()
{
    return new T();
}

The compiler will convert the “return new T();” to call “CreateInstance”.

In general, there is no use for the CreateInstance in application code, because the type must be known at compile time. If the type is known at compile time, normal instantiation syntax can be used (new operator in C#, New in Visual Basic, gcnew in C++).

More Info: http://msdn.microsoft.com/en-us/library/0hcyx2kd.aspx

Leave a Comment