如何将项目添加到通用集合中?

发布于 2024-12-26 01:42:18 字数 688 浏览 0 评论 0 原文

给定:

public static void DoStuff<T>(ICollection<T> source)
{
    Customer c = new Customer();
    ....
    source.Add(c);
}

除了 c 不是 类型。

那么如何将项目添加到通用集合中?


尝试有:

public static void DoStuff(ICollection<Human> source)
{
    Customer c = new Customer();
    ....
    source.Add(c);
}

但我不使用它,因为没有人可以调用DoStuff

ICollection<Customer> c;
DoStuff(c); <---error

因为有关协方差的东西,而.NET没有意识到Customer 源自 Human

class Customer : Human {}

Given:

public static void DoStuff<T>(ICollection<T> source)
{
    Customer c = new Customer();
    ....
    source.Add(c);
}

except c is not of type <T>.

So how do i add an item to a generic collection?


i tried having:

public static void DoStuff(ICollection<Human> source)
{
    Customer c = new Customer();
    ....
    source.Add(c);
}

but i don't use it because nobody can call DoStuff:

ICollection<Customer> c;
DoStuff(c); <---error

because something about covariance, and .NET doesn't realize that Customer descends from Human:

class Customer : Human {}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

醉殇 2025-01-02 01:42:18

只是为了让您知道为什么会收到该错误,ICollection 无法传递给 ICollection,因为它们不是同样的事情。可以这样想,如果您有一个 ICollection,如果 Deadbeat 派生自 Add(new Deadbeat()) >人类。

关于如何避免泛型问题的其他答案解决了您的问题(因此他们应该获得答案):

public static void DoStuff<T>(ICollection<T> source) where T : new()
{
    T c = new T();
    ...
    source.Add(c);
}

但我只是想抛出这个答案来解释为什么您会收到该错误。想一想,如果您可以将 Customer 集合作为 Human 集合传递,那么您将可以添加任何类型的人类,而这将违反原始集合。

因此,即使 Customer 扩展了 Human,这并不意味着 ICollection 扩展了 ICollection并且 ICollection 不是协变/逆变,因为它对 inin 都使用 T out 操作。

Just so you know why you get that error, an ICollection<Customer> can not be passed to an ICollection<Human> because they are not the same thing. Think of it this way, if you had an ICollection<Human> you could Add(new Deadbeat()) if Deadbeat derived from Human.

The other answers on ways to avoid your issue with a generic solves your problem (so they should get the answer credit):

public static void DoStuff<T>(ICollection<T> source) where T : new()
{
    T c = new T();
    ...
    source.Add(c);
}

but I just wanted to throw this answer out to explain why you get that error. Think of it, if you could pass a collection of Customer as a collection of Human, it would let you add ANY kind of human, and this would violate the original collection.

Thus, even though Customer extends Human, this does not mean that ICollection<Customer> extends ICollection<Human> and ICollection<T> is not covariant/contravariant because it uses T for both in and out operations.

耶耶耶 2025-01-02 01:42:18

应该是:

T t = new T();
....
source.Add(t);

此外,您还必须添加 new 约束保证 T 具有公共无参数构造函数:

public static void DoStuff<T>(ICollection<T> source) where T: new()
{
   //...
}

should be just:

T t = new T();
....
source.Add(t);

Also you will have to add the new constraint to guarantee that T has a public parameterless constructor:

public static void DoStuff<T>(ICollection<T> source) where T: new()
{
   //...
}
情魔剑神 2025-01-02 01:42:18

你可能想要这样的东西:

public static void DoStuff<T>(ICollection<T> source)
    where T : new()
{
    T c = new T();
    source.Add(c);
}

You probably want something like this:

public static void DoStuff<T>(ICollection<T> source)
    where T : new()
{
    T c = new T();
    source.Add(c);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文