c# - 如何为 List<T> 创建新的通用项?

标签 c# generics .net-4.0 refactoring

给定可能包含 5 个项目的列表,下面的方法将返回恰好包含 5 个项目的新列表(如果原始列表少于 5 个,则添加更多项目)。这工作正常,但现在我需要重构它,以便它可以处理通用 T 列表(一个具有相同年份和 Cnt 属性的列表)。如何转换此方法以接受一个列表并让它返回一个包含 5 个元素的新列表?

    private static List<FiveYearComplex> CreateFiveYearTemplate(int startYear, 
        int endYear, ObjectResult<FiveYearComplex> result)
    {
        var list = new List<FiveYearComplex>(5);

        for (int year = startYear; year < endYear; ++year)
        {
            list.Add(new FiveYearComplex() { Year = year, Cnt = 0 });
        }
        FiveYearComplex tmpItem;
        foreach (var item in result)
        {
            tmpItem = list.Find(w => w.Year == item.Year);
            if (tmpItem == null)
            {
                tmpItem = new FiveYearComplex() { Cnt = 0, Year = item.Year };
            }
            else
            {
                tmpItem.Cnt = item.Cnt;
            }
        }
        return list;
    }

当我尝试使用 List 时,我最终遇到了这个部分:

for (int year = startYear; year < endYear; ++year)
{
    list.Add(new T() { Year = year, Cnt = 0 });
}

我得到一个错误...

谢谢!

为了完整性:

    public interface IYearTemplate
    {
        int? Year { get; set; }
        decimal? Cnt { get; set; }
    }

    private static List<T> CreateFiveYearTemplate <T> (
        int startYear, int endYear, 
        ObjectResult<FiveYearAttendanceComplex> result) 
        where T : IYearTemplate, new()
    {
        var list = new List<T>(5);

        for (int year = startYear; year < endYear; ++year)
        {
            list.Add(new T() { Year = year, Cnt = 0 });
        }
        T tmpItem;
        foreach (var item in result)
        {
            tmpItem = list.Find(w => w.Year == item.Year);
            if (tmpItem == null)
            {
                tmpItem = new T() { Cnt = 0, Year = item.Year };
            }
            else
            {
                tmpItem.Cnt = item.Cnt;
            }
        }
        return list;
    }

谢谢。

最佳答案

您不能轻松地将您的方法转换为处理泛型列表,因为您的方法不是泛型的。它要求列表中的每个项目都具有属性 CntYear,为了使您的方法通用,您必须添加此约束。

public interface IYearTemplate
{
   int Cnt {get;set;}
   int Year {get;set;}
}

您的方法还需要一个默认构造函数,它表示为约束 new() - 所以它可能看起来像这样:

private static List<T> CreateFiveYearTemplate<T>(int startYear,
    int endYear, ObjectResult<T> result) where T: IYearTemplate, new()
{
    var list = new List<T>(5);

    for (int year = startYear; year < endYear; ++year)
    {
        list.Add(new T() { Year = year, Cnt = 0 });
    }
    T tmpItem;
    foreach (var item in result)
    {
        tmpItem = list.Find(w => w.Year == item.Year);
        if (tmpItem == null)
        {
            tmpItem = new T() { Cnt = 0, Year = item.Year };
        }
        else
        {
            tmpItem.Cnt = item.Cnt;
        }
    }
    return list;
}

话说这个方法看起来不是很通用,因为约束非常具体。你为什么让它通用?

关于c# - 如何为 List<T> 创建新的通用项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5851044/

相关文章:

c# - ListBox 上的 WPF 动画

C++ 泛型插入排序

c#-4.0 - CultureInfo.Name 属性的最大长度

c# - 在 N 层应用程序中使用 DTO 时的 OrderBy

c# - 将状态更新从 C++ 中的函数发送到 C#

typescript - 如何在 TypeScript 中过滤类似对象的联合类型?

Java Iterable<Iterable<T>> 到 ArrayList<ArrayList<T>>

c# - 控制台应用程序 C# 的关闭事件

c# - 防止窗口跨越多个显示器

c# - 对 DFS 和 BFS 程序有用的 C# 类/方法