locked
How can I make a generic method where you pass a generic type and List<T> as a method parameter? RRS feed

  • Question

  • How can I convert this code to accept a string or integer list type? Right now I am passing List<string> but is there a way to pass the type also?  like List<type> ?

    thanks.


     
            public static void FillList_Column(OleDbDataReader rdr, string ColumnName, Boolean isLowerCase, out List<string> myList)  
            {  
                Debug.Assert(ColumnName != "");  
                Debug.Assert(rdr != null);  
     
                myList = new List<string>();  
     
                if (rdr.HasRows)  
                {  
                    while (rdr.Read())  
                    {  
                        if (isLowerCase)  
                            myList.Add(rdr[ColumnName].ToString().ToLower());   // Faster rdr.GetString[0]...  
                        else  
                            myList.Add(rdr[ColumnName].ToString());  
                    }  
                }  
            } 
    Monday, September 22, 2008 3:11 PM

Answers

  • You don't need to add the generic restraint new() unless you're going to return a new T, as calling "new" on a List<T> calls the constructor of the List<T>, not the "T". The following illustrates the difference:

    public List<T> ReturnList<T>()  
    {  
        return new List<T>();  
    }  
     
    public T ReturnValue<T>() where T : new()  
    {  
        return new T();  

    David Morton - http://blog.davemorton.net/
    • Marked as answer by Zhi-Xin Ye Friday, September 26, 2008 10:02 AM
    Monday, September 22, 2008 9:08 PM

All replies

  • I'm not sure I completely understand what it is you're trying to do, but your method doesn't seem well-suited for generics.  While you could set up your method to be generic, I think you would do far better to create a second overload of this particular method that takes a List<Type> and returns a List<Type> from the method via the out keyword, instead of creating a generic method. Nevertheless, creating generic types in methods is as simple as doing the following:

    public static List<T> MyGenericMethod<T>(List<T> list)
    {
        List<T> result = new List<T>();
        // fill the result with objects of type T. 
        return result;
    }

    Notice the declaration of the type parameter immediately after the method name declaration, and before the portion of the method signature that accepts your parameters.  That's how you define generic parameters.

    That being said, the nature of your method is not truly "generic", as you're adding strongly typed "strings" to the list.  a List<T> won't accept a string unless you restrict T to "string" which would defeat the whole purpose.
    David Morton - http://blog.davemorton.net/
    Monday, September 22, 2008 3:22 PM
  • David,
    If the method returns a "new" List<T>(), do you need to add the generic constraint...

        where T : new()

    ?
    Les Potter, Xalnix Corporation, Yet Another C# Blog
    Monday, September 22, 2008 8:45 PM
  • You don't need to add the generic restraint new() unless you're going to return a new T, as calling "new" on a List<T> calls the constructor of the List<T>, not the "T". The following illustrates the difference:

    public List<T> ReturnList<T>()  
    {  
        return new List<T>();  
    }  
     
    public T ReturnValue<T>() where T : new()  
    {  
        return new T();  

    David Morton - http://blog.davemorton.net/
    • Marked as answer by Zhi-Xin Ye Friday, September 26, 2008 10:02 AM
    Monday, September 22, 2008 9:08 PM