gpt4 book ai didi

c# - 为什么这个 C# 方法重载没有按预期工作?

转载 作者:行者123 更新时间:2023-11-30 14:25:13 24 4
gpt4 key购买 nike

<分区>

我有以下方法:

public void Say<TItem>(TItem item)
{
Console.WriteLine("A");
}

public void Say<TItem>(IEnumerable<TItem> items)
{
Console.WriteLine("B");
}

当我运行以下命令时:

void Main()
{
Say<string>("Foo");
Say(new string[] { "Foo", "Bar"});

}

第一个方法被调用了两次:

A
A

注意更改 IEnumerable<T>TItem[]确实按预期工作,但在 IEnumerable<T> 的情况下为什么不采用第二种方法?我怎样才能实现预期的重载?

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com