简体   繁体   中英

How to make a function which returns Type <T>

C# .net 4.0

I have created a function which converts ArrayList into List

List<MatchBowlDetails> lstBowling = ConvertArrayListToList(BowlingTeamScore).Cast<MatchBowlDetails>().ToList(); 

public static List<Object> ConvertArrayListToList(CollectionClass paramcollection)
    {
        //Function use:
        //This function takes array List and return List<Type>
        //TODO

        return null;
}

As you can see here I have created a function which converts ArrayList into List<T>

Here BowlingTeamScore is a collection of MatchBowlDetails class and inherited from CollectionClass which make ArrayList type of collection.

Now here as this function is returning List<Object> type of object and I want to return List<T> type of T , where T is paramCollection.GetType();

So the new user code will be :

List<MatchBallDetails> lstBowling = ConvertArrayListToList<BowlingTeamScore); // here no need to type cast back..

Please suggest how to do this.

Do you mean something like this?

public static List<T> ConvertArrayListToList<T>(CollectionClass paramcollection)
{
    return paramcollection.Cast<T>().ToList(); 

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM