Generic or Non-Generic?

A

André Nobre

Hi all,
I´m creating a factory class and I don´t know if it´s better to create it
with Generics or not. The basic funcionality would be:
public static class DalcFactory<T>

public static T Instantiate()

{

if (typeof(T).Name.Equals("Example"))

return (T) Assembly.LoadFrom(...).CreateInstance(...);

}

Without generics, the class would contains inumerous static methods,
GetObject1, GetObject2, GetObject3, and others. With Generics, I would use
just one method, Instantiate, as above.

I want to know wich one is better, in performance.

Thanks!

André
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

If you use separate methods, the type will be identified at compile time
instead of at runtime, so you'll save a bit of work.

I think that the work to identify the type is relatively small compared
to the work of creating the object, though, so I'd suggest that you go
for the method that gives the nicest code.
 

Ask a Question

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.

Ask a Question

Top