D
Dan Holmes
Isn't CreateInstance(typeof(int)) the same as CreateInstance<int>()?
i don't understand how this method helps anything.
dan
i don't understand how this method helps anything.
dan
Dan said:Isn't CreateInstance(typeof(int)) the same as CreateInstance<int>()?
i don't understand how this method helps anything.
dan
I guess i figured the non-generic version could do all of that too.antoan said:CreateInstance<T> is an example of a generic method. The generic
parameter T enables you to invoke it with different data types eg:
CreateInstance<CustomDataType>()
CreateInstance<string>()
CreateInstance<T>()
An advantage of a generic method is that you can implement the
behaviour/algorithm once and apply it to different types.
Hope this helps.
A
I guess i figured the non-generic version could do all of that too.
Dan Holmes said:Isn't CreateInstance(typeof(int)) the same as CreateInstance<int>()?
i don't understand how this method helps anything.
Chris said:I really cannot see the point of the generic version of CreateInstance.
All it appears to be to me is an unsafe way to call new T() as it
doesn't enforce that T has a parameterless constructor.
Thus:
String a = Activator.CreateInstance<String>();
will compile but you just get a run-time error.
But if you knew the type to put in the generic why not just call:
MyClass a = new MyClass()
That being said, I can't see a reason to EVER use
Activator.CreateInstance<T>.
That being said, I can't see a reason to EVER use
Activator.CreateInstance<T>.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.