使具体的返回类型通用

时间:2013-07-03 20:37:54

标签: c# generics interface

考虑以下两种方法,它们本质上是彼此的复制和粘贴版本

public static List<Book> GetSubSet(List<Book> incomingList)
        {
            List<Book> returnList = new List<Book>();
            Random r = new Random();
            Console.WriteLine("Enter size of random subset: ");
            int randomInt = 0;
            int size = Convert.ToInt32(Console.ReadLine());
            while (size > incomingList.Count)
            {
                Console.WriteLine("Size too large, enter smaller subset: ");
                size = Convert.ToInt32(Console.ReadLine());
            }
            while (returnList.Count < size)
            {
                randomInt = r.Next(incomingList.Count);
                if (!returnList.Contains(incomingList[randomInt]))
                {
                    returnList.Add(incomingList[randomInt]);
                }
            }
            return returnList;
        }

 public static List<Pet> GetSubSet(List<Pet> incomingList)
        {
            Random r = new Random();
            List<Pet> returnList = new List<Pet>();
            Console.WriteLine("Enter random subset size: ");
            int size = Convert.ToInt32(Console.ReadLine());
            while (size > incomingList.Count)
            {
                Console.WriteLine("Size too large.  Enter random subset size: ");
                size = Convert.ToInt32(Console.ReadLine());
            }
            while (returnList.Count < size)
            {
                int randomInt = r.Next(incomingList.Count);
                if (!returnList.Contains(incomingList[randomInt]))
                {
                    returnList.Add(incomingList[randomInt]);
                }
            }

            return returnList;
        }

显然这不是OO,我想有一个方法可以处理任何类型。我不想使用继承,因为没有逻辑连接或书籍和狗之间,我不希望包含类型因为包含也没有意义。所以我们留下了一个界面。像

这样的东西
 interface ISubset
    {

        IEnumerable<T> GetSubset();
    }

我见过,但我不知道如何让尖括号接受任何泛型类型。如何使IEnumerable类型通用,以便我可以在任何类上实现GetSubset方法?

3 个答案:

答案 0 :(得分:4)

您可以将GetSubSet方法设为通用:

public static List<T> GetSubSet<T>(List<T> incomingList)
{
    var returnList = new List<T>();
    Random r = new Random();
    Console.WriteLine("Enter size of random subset: ");
    int randomInt = 0;
    int size = Convert.ToInt32(Console.ReadLine());
    while (size > incomingList.Count)
    {
        Console.WriteLine("Size too large, enter smaller subset: ");
        size = Convert.ToInt32(Console.ReadLine());
    }

    while (returnList.Count < size)
    {
        randomInt = r.Next(incomingList.Count);
        if (!returnList.Contains(incomingList[randomInt]))
        {
            returnList.Add(incomingList[randomInt]);
        }
    }

    return returnList;
}

然后你可以像这样使用它:

List<Pet> incomingList = ...
List<Pet> result = GetSubSet(incomingList);

请注意,由于方法的编译器泛型类型推断,我们在调用GetSubSet方法时没有指定类型。这相当于:

List<Pet> result = GetSubSet<Pet>(incomingList);

但编译器能够推断出。

答案 1 :(得分:4)

使其成为通用:

    public static List<T> GetSubSet<T>(List<T> incomingList)
    {
        Random r = new Random();
        List<T> returnList = new List<T>();
        Console.WriteLine("Enter random subset size: ");
        int size = Convert.ToInt32(Console.ReadLine());
        while (size > incomingList.Count)
        {
            Console.WriteLine("Size too large.  Enter random subset size: ");
            size = Convert.ToInt32(Console.ReadLine());
        }
        while (returnList.Count < size)
        {
            int randomInt = r.Next(incomingList.Count);
            if (!returnList.Contains(incomingList[randomInt]))
            {
                returnList.Add(incomingList[randomInt]);
            }
        }

        return returnList;
    }

答案 2 :(得分:1)

有一种方法,并使方法本身通用。

public static List<T> GetSubSet<T>(List<T> incomingList)