Is there any difference between following two ways of creating an object.
Student s1 = Activator.CreateInstance<Student>();
Student s1 = new Student();
- Is there any difference in the way constructor is called or in initializing the memory?
- Per my understanding, the first method looks completely redundant. If the programmer knows data type during design time, he will use the second method.
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:
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