在 C# 中可以使用 {a,b,c} 实例化队列吗?

发布于 2024-09-29 13:40:19 字数 139 浏览 10 评论 0原文

在 C# 中可以做到吗?

Queue<string> helperStrings = {"right", "left", "up", "down"};

或者我必须首先为此生成一个数组?

Is it possible to do that in C#?

Queue<string> helperStrings = {"right", "left", "up", "down"};

or do I have to produce an array first for that?

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

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

发布评论

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

评论(4

南七夏 2024-10-06 13:40:19

不,你不能以这种方式初始化队列。

不管怎样,你可以做这样的事情:

var q = new Queue<string>( new[]{ "A", "B", "C" });

显然,这意味着传递一个数组。

No you cannot initialize a queue in that way.

Anyway, you can do something like this:

var q = new Queue<string>( new[]{ "A", "B", "C" });

and this, obviously, means to pass through an array.

没︽人懂的悲伤 2024-10-06 13:40:19

在 C# 中可以做到这一点吗?

不幸的是没有。

C# 中集合初始值设定项的规则是对象必须 (1) 实现 IEnumerable,并且 (2) 具有 Add 方法。集合初始值设定项

new C(q) { r, s, t }

被重写为

temp = new C(q);
temp.Add(r);
temp.Add(s);
temp.Add(t);

,然后产生 temp 中的任何内容。

Queue 实现了 IEnumerable,但它没有 Add 方法;它有一个入队方法。

Is it possible to do that in C#?

Unfortunately no.

The rule for collection initializers in C# is that the object must (1) implement IEnumerable, and (2) have an Add method. The collection initializer

new C(q) { r, s, t }

is rewritten as

temp = new C(q);
temp.Add(r);
temp.Add(s);
temp.Add(t);

and then results in whatever is in temp.

Queue<T> implements IEnumerable but it does not have an Add method; it has an Enqueue method.

晒暮凉 2024-10-06 13:40:19

由于 Queue 未实现“Add”方法,因此您需要实例化一个 IEnumerable,从中对其进行初始化:

Queue<string> helperStrings 
    = new Queue<string>(new List<string>() { "right", "left", "up", "down" });

As Queue<T> does not implement an 'Add' method, you'll need to instantiate an IEnumerable<string> from which it can be initialized:

Queue<string> helperStrings 
    = new Queue<string>(new List<string>() { "right", "left", "up", "down" });
赠我空喜 2024-10-06 13:40:19

确切的答案是:是的。
正如 Eric Lippert 所说,它实际上是语法糖,是编译器的语法分析,所以这意味着如果可以让编译器知道 Queue 有一个 Add 方法,然后它就可以“欺骗编译器”。
“扩展方法”可以做到:

public static class QueueHacky
{
    public static void Add<T>(this Queue<T> queue, T newItem)
    {
        queue.Enqueue(newItem);
    }
}

然后,这就合法了:

Queue<string> helperStrings = new Queue<string> { "right", "left", "up", "down" };

警告:这是一个 Hacky 解决方案,它只是为了解决这个问题,不推荐使用,它有代码味道。

The exact answer is: yes.
As Eric Lippert said, it is actually the syntactic sugars, is the syntax analysis of the compiler, so it means that if the compiler can be made aware that Queue has an Add method, then it can "fool the compiler".
"Extension method" can made it:

public static class QueueHacky
{
    public static void Add<T>(this Queue<T> queue, T newItem)
    {
        queue.Enqueue(newItem);
    }
}

Then, this becomes legal:

Queue<string> helperStrings = new Queue<string> { "right", "left", "up", "down" };

Warning: This is a Hacky solution, it just for solved this question, not recommend to use, it has code smell.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文