简体   繁体   English

VB.Net等效于Indexer Initializer

[英]VB.Net equivalent for Indexer Initializer

Is there any VB.NET equivalent to the C#6 Indexer Initializer construct? 是否有任何与C#6 Indexer Initializer构造等效的VB.NET?

var placesByZip = new Dictionary<string, string>
     {
         ["63368"] = "Dardenne Prairie",
         ["63141"] = "Des Peres",
         ["63101"] = "St. Louis"
     };

--Edit-- - 编辑 -

Just to be more precise, I'm looking for the Indexer Initializer (which is an object initializer), not the collection initializer. 准确地说,我在寻找Indexer Initializer(这是一个对象初始化器),而不是集合初始化器。

More information on the difference can be found near the end of this blog entry . 有关该区别的更多信息可以在此博客文章的结尾处找到。

-- Edit2: -编辑2:

From the blog mentionned, the solution should allow to do this: 从提到的博客中,解决方案应允许这样做:

Again, I wish to reiterate that the Indexer Initializer is not a type of Collection Initializer, it's a type of Object Initializer. 再一次,我想重申一下,索引器初始化器不是集合初始化器的一种,而是对象初始化器的一种。 Why do I keep saying this? 为什么我一直这样说? Why is this distinction important? 为什么这种区别很重要? Because you cannot mix object initializers and collection initializers in the same initialization list. 因为您不能在同一初始化列表中混合使用对象初始值设定项和集合初始值设定项。

So, we can't initialize a collection and set a property in the same initialization list, but we can set a property, field, and indexer in the same initialization list since they are all valid Object Initializers. 因此,我们无法在相同的初始化列表中初始化集合并设置属性,但是我们可以在同一初始化列表中设置属性,字段和索引器,因为它们都是有效的对象初始化器。

It should be 它应该是

Dim placesByZip as New Dictionary(Of String, String) From {
{"63368", "Dardenne Prairie"}, 
{"63141", "Des Peres"},
{"63101", "St. Louis"}
}

(never tried it before) (从未尝试过)

It is 它是

    Dim placesByZip As New Dictionary(Of String, String) From {
        {"63368", "Dardenne Prairie"},
        {"63141", "Des Peres"},
        {"63101", "St. Louis"}}

Collection Initializers 集合初始化器

As of 2019-06-27, VB.NET version 15.8 (VS 2017), object initialization from indexed members is not authorized according to Microsoft documentation on object initializers . 截至2019年6月27日,VB.NET版本15.8(VS 2017),根据Microsoft关于对象初始化器的文档,未授权从索引成员进行对象初始化

Class members being initialized cannot be indexed or qualified 初始化的类成员不能被索引或限定

For completeness sake, neither is it mentioned in collection initializers documentation . 为了完整起见, 集合初始化器文档中都未提及。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM