What is the diffrenece between the two examples below? What is the purpose of Activator, why would I use it?
1.
string objTypeName = "Foo";
Foo foo = (Foo)Activator.CreateInstance(Type.GetType(objTypeName));
2.
Foo foo = new Foo()
I went through below the pages linked below, but didn’t get a clear picture.
If you know the type that you will instantiate at design time then there is no need to use
Activator.CreateInstance. AllActivator.CreateInstancedoes is call the public default constructor.Activator.CreateInstanceis one form of “Late Binding” that can be useful if you want to instantiate aTypethat you do not know at design time. If you already know the type of the concrete class at design time then you definitely want to instantiate it using “Early Binding”, its much faster and simpler, and obvious. i.e. call the appropriate constructor directly.