C# Generic new() constructor problem

Posted by LB on Stack Overflow See other posts from Stack Overflow or by LB
Published on 2009-05-08T15:04:29Z Indexed on 2010/04/30 17:47 UTC
Read the original article Hit count: 100

Filed under:
|
|

I'm trying to create a new object of type T via its constructor when adding to the list.

I'm getting a compile error: The correct error message is: 'T': cannot provide arguments when creating an instance of a variable

But it does! Any ideas?

public static string GetAllItems<T>(...) where T : new()
{
   ...
   List<T> tabListItems = new List<T>();
   foreach (ListItem listItem in listCollection) 
   {
       tabListItems.Add(new T(listItem)); // error here.
   } 
   ...
}

© Stack Overflow or respective owner

Related posts about c#

Related posts about generics