简体   繁体   中英

How to pass a parameter to declare generic type list?

private void listHelper (int listSize, ref Object taker, Object giver, Type type)
    {
        if (listSize == 0)
        {
            taker = null;
        }
        else
        {
            taker = new List<type>(listSize);
            giverType tempGiver;
            for (int i = 0; i < listSize; i++)
            {
                type= new type();
                tempGiver.value = giver[i];
                taker.Add(tempGiver);
            }
        }
    }

This is my code. I have tried to pass a type parameter to the private method to declare generic type list... But I have no idea what to do.

parameter taker is a list. In this case, taker is storing a generic type of object. parameter type is a type of the generic.

I have searched about this on the internet for a while. I am still not sure how to do this.

Try making a generic method instead:

private void listHelper<T> (int listSize, out List<T> taker, IList<T> giver)
{
    if (listSize == 0)
    {
        taker = null;
    }
    else
    {
        taker = new List<T>(listSize);
        for (int i = 0; i < listSize; i++)
        {
            taker.Add(giver[i]);
        }
    }
}

Also I suggest using out rather than ref (as I have done) since you always assign a value to taker before returning. This allows the calling code to not have to assign a dummy value before calling your method.

Or better yet, you should just return the List !

private List<T> listHelper<T> (int listSize, IList<T> giver)
{
    List<T> taker;
    ...
    return taker;
}

Note, if all your method is doing is copying all elements from giver to taker (that is, specifying listSize is really just a holdover habit from coding C and always equals the number of elements in giver ), you can either substitute giver.Count or just do:

private List<T> listHelper<T> (IList<T> giver)
{
    return giver.Any() ? giver.ToList() : null;
}

Of course, if listSize is really "number of elements to return, starting at the front of giver ", you can do:

private List<T> listHelper<T> (IList<T> giver, int numElements)
{
    return giver.Any() ? giver.Take(numElements).ToList() : null;
}

In fact, reading into your question further, it looks like you want to instantiate a new object of some type and set it's value property to the item in giver . So how about this (of course I'm assuming a lot about your taker object and that you have an interface ITaker<U> which specifies a field/property public U value ):

private List<T<U>> listHelper<T, U> (IList<U> giver) where T : ITaker<U>, new()
{
    return giver.Any() ? giver.Select(g => new T<U>() {value = g}).ToList() : null;
}

I guess its something like:

private void ListHelper<T>(int listSize, ref object taker, object giver)
{
    ...
    List<T> list = new List<T>();
    ...
}

You should create a Generic method. Generics act as templates in C#. The method is bound when it's declared.

public void ListHelper<T>//...

    List<T>  my list//...

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