2

下面的代码给了我一个关于 IEnumerable 可能的多重枚举的警告:

public ClassName(IEnumerable<OtherClassName> models) : base(models)
{
    _models.AddRange(models);
}

由于“基本”调用,消除此警告的正常解决方案不起作用。我无法转换为列表,因为没有地方可以存储该列表。

我唯一的选择是让构造函数将列表作为参数吗?在这种情况下,这是推荐的做法吗?

4

1 回答 1

6

Create another private constructor which takes List<OtherClassName> and call it using this keyword:

public ClassName(IEnumerable<OtherClassName> models)
    : this(models.ToList())
{
}

private ClassName(List<OtherClassName> models)
    : base(models)
{
    _models.AddRange(models);
}

And make sure you really need to store models within your class. Maybe it's already stored in base class and you can use it from there?

于 2013-09-01T19:39:01.153 回答