Yes, but there are some caveats. First, you have to put the new
constraint on the function, like so:
public T MyFunction<T>(...) where T : new()
{
}
That will allow you to put code like this in your function:
public T MyFunction<T>(...) where T : new()
{
return new T();
}
In this case, T must have a parameterless (default) constructor.
Now, if you want to set values on T, you have to have some idea in the
function what T is. T will have to implement an interface and you have to
put the constraint on T in the function, like so:
public T MyFunction<T>(...) where T : IMyInterface, new()
{
}
Then, you can access instances of T as if they defined IMyInterface.
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
-
mv*@spam.guard.caspershouse.com
<fo*******@hotmail.comwrote in message
news:11**********************@n76g2000hsh.googlegr oups.com...
It is something like
Public T myFunc<T>(Type type_Of_T, other params but none is for type
T...)
{
// create an instance of T
// do some other initialization for the instance of T
// return the instance of T
}