The closest available is Activator.CreateInstance
:
object o = Activator.CreateInstance(type);
... but of course this relies on there being a public parameterless constructor. (Other overloads allow you to specify constructor arguments.)
I've used an explicitly typed variable here to make it clear that we really don't have a variable of the type itself... you can't write:
Type t = typeof(MemoryStream);
// Won't compile
MemoryStream ms = Activator.CreateInstance(t);
for example. The compile-time type of the return value of CreateInstance
is always object
.
Note that default(T)
won't create an instance of a reference type - it gives the default value for the type, which is a null reference for reference types. Compare that with CreateInstance
which would actually create a new object.