集合初始化语法,有什么用?

发布于 2024-09-28 15:32:10 字数 797 浏览 1 评论 0原文

我们可以使用类似的函数

public static List<T> New<T>(params T[] items) {
    return new List<T>(items);
}

,更重要的是它更好

var list = new List<int> {1,2,3};
var list = List.New(1,2,3);

那么,什么时候我们真正需要使用它呢?

字典

public static Dictionary<T, K> New<T, K>(T key, K value) {
    return new Dictionary<T,K>().FAdd(key, value);
}

var dict = new Dictionary<int, string>(){
 {1, "a"},
 {2, "b"}
};

var dict = Dict.New(1,"a").FAdd(2, "b");

为什么更好,类型推理

var x = new Dictionary<string, List<int>>(){
   {"a", new List<int>{1}},
   {"b", new List<int>{2}},
}

var x = Dict.New("a", List.New(1)).FAdd("b", List.New(2));

We just can use function like

public static List<T> New<T>(params T[] items) {
    return new List<T>(items);
}

and more important it's better

var list = new List<int> {1,2,3};
var list = List.New(1,2,3);

So, when we really need to use it?

Dictionary

public static Dictionary<T, K> New<T, K>(T key, K value) {
    return new Dictionary<T,K>().FAdd(key, value);
}

var dict = new Dictionary<int, string>(){
 {1, "a"},
 {2, "b"}
};

var dict = Dict.New(1,"a").FAdd(2, "b");

Why it's better, type inference

var x = new Dictionary<string, List<int>>(){
   {"a", new List<int>{1}},
   {"b", new List<int>{2}},
}

var x = Dict.New("a", List.New(1)).FAdd("b", List.New(2));

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

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

发布评论

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

评论(3

岛徒 2024-10-05 15:32:10

这需要在您的类型中添加一个新函数。集合初始值设定项不需要任何外部代码即可工作 - 您可以将它们用于任何兼容的集合类型,并且它由编译器自动处理。

此外,使用语言功能而不是滚动自己的方法将使代码对其他开发人员来说更加明显。由于您使用的是标准语言功能,因此意图变得显而易见。使用扩展方法可以轻松掩盖您的意图。

那么,什么时候我们真正需要使用它呢?

您永远不需要使用它。与所有语言功能一样,它是一种工具 - 作为开发人员,您可以自行决定使用或忽略。

This requires adding a new function in your type. Collection initializers don't require any external code in order to work - you can use them for any compatible collection type, and it's handled by the compiler automatically.

In addition, using the language functionality instead of rolling your own method will make the code more obvious to other developers. Since you're using a standard language feature, the intent becomes obvious. Using an extension method can easily mask your intent.

So, when we really need to use it?

You never need to use it. Like all language features, it's a tool - you can choose to use or to ignore, at your discretion as a developer.

本宫微胖 2024-10-05 15:32:10

您的示例的一个问题是它的效率比集合初始值设定项语句低得多。使用 params 会强制集合本质上创建两次

  1. 。一次为值创建 .Net 数组。
  2. 创建实际集合

另一方面,集合初始值设定项只需创建第二个集合。

总的来说,集合初始化器的主要原因之一是不必编写此 API。框架中有各种各样的集合,但不幸的是,它们的创建模式多种多样。许多没有采用 IEnumerable 的构造函数,而是强制执行一组单独的 Add 调用。你最终会一遍又一遍地编写这个方法。

集合初始值设定项可让您避免完全编写它,而是直接使用开箱即用的集合。

One issue with your sample is that it's much less effecient than a collection initializer statement would be. The use of params forces the collection to essentially be created twice

  1. Once to create a .Net Array for the values
  2. Creating the actual collection

Collection initializers on the other hand just create the second collection.

Overall though one of the primary reasons for collection initializers is not having to write this API. There are a variety of collections in the framework with an unfortunate variety of creating patterns. Many don't have the constructor taking IEnumerable and instead force a set of individual Add calls. You end up writing this method over and over again.

Collection initializers let you avoid writing it altogether and instead just use the collection out of the box.

初见你 2024-10-05 15:32:10

不,这并没有更好。集合初始化语法支持任何类:1)具有Add方法2)实现IEnumerable。因此,它适用于每个集合,甚至适用于 Dictionary,而不仅仅是 List。另外,您的方法需要创建数组,开销虽小但过多。初始化没有这个。

No, it is not better. Collection Initialization Syntax support any class that: 1) Has Add method 2) Implements IEnumerable. Thus it works with every collection even with Dictionary, not List only. Plus your method requires creation of array, small but excessive overhead. Initialization doesn't have it.

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