Generic or TypeCast - Pann's Thoughts

Generic or TypeCast

Let’s say I have a factory method that creates an instance of an object (e.g. I wrap Activator.CreateInstance).

I have 2 options

1) public object Create (string className) {return Activator.Create…….;}

2) public T Create<T> (string className) {return (T) Activator.Create……;}

The client code will then be one of

1) MyObj obj= (MyObj)Create (“myObjKey”);

2) MyObj obj= Create<MyObj> (“myObjKey”);

Which would you prefer ?

Comments

No Comments