简体   繁体   English

什么是C#中的Ruby哈希的标准等效项-如何在C#中创建像Ruby这样的多维Ruby哈希

[英]What is the standard equivalent of Ruby Hash in C# - How to create a multi dimensional Ruby like hash in C#

I have prior experience in Ruby and wanted to learn a compiled language and have just picked up C# recently. 我以前有Ruby的经验,想学习一种编译语言,最近才开始学习C#。 I am wondering what is the Ruby Equivalent of Hashes in C#. 我想知道什么是C#中的哈希的Ruby等效项。 Eg, how can i create a something similar to a Ruby hash below, in C#? 例如,如何在C#中创建类似于下面的Ruby哈希的东西?

x = {"a"=>{"b"=>"c","d"=>["e","f",{"g"=>1}]}

What is the standard way to create something like this in C#? 在C#中创建这样的东西的标准方法是什么? Should I use hashtable, dictionary or objects? 我应该使用哈希表,字典还是对象?

Short answer: There is none. 简短的回答:没有。

Long answer: 长答案:

The closest C# equivalent is the Dictionary . 与C#最接近的等效项是Dictionary

You can use it like so: 您可以这样使用它:

var myDict = new Dictionary<string, int>();

// Adding values
myDict.Add("foo", 3);
myDict["bar"] = 8; // Alternative syntax

// Getting values
int bar =  myDict["bar"];
bool fooExists = myDict.ContainsKey("foo"); // true

// Safe get
int foo;
bool ableToGetFoo = myDict.TryGetValue("foo", out foo);

// Splitting
var allKeys = myDict.Keys;
var allValues = myDict.Values;

However, this would not work for you . 但是, 这对您不起作用 At least, not in any straightforward way. 至少不是以任何直接的方式。

You have to realize that C# is a strongly typed language. 您必须意识到C#是一种强类型语言。 This means that C#'s dictionaries, like all other collections, have a limitation: all elements must be of the same type. 这意味着C#的字典与所有其他集合一样都有局限性:所有元素都必须是同一类型。 This means that all the keys must be the same type as each other, and all the values must also be the same type as each other. 这意味着所有键的类型必须彼此相同,并且所有值的类型也必须彼此相同。 That is simply how strongly typed languages work. 这就是强类型语言工作的方式。

If you want to re-create your example in C#, you'll have to define a custom data structure to accommodate it. 如果要在C#中重新创建示例,则必须定义一个自定义数据结构来容纳它。 However, all the nesting that is present in your example will make that challenging. 但是,示例中存在的所有嵌套都将带来挑战。 Some elements of it, such as this particular bit: 它的某些元素,例如此特定位:

["e","f",{"g"=>1}]

..are flat out impossible. ..完全不可能。 An array or list cannot have elements of completely different types. 数组或列表不能具有完全不同类型的元素。

Here is my attempt at creating something similar to your example: 这是我尝试创建类似于您的示例的内容:

var x = new Dictionary<string, Dictionary<string, List<string>>>()
{
    {
        "a", new Dictionary<string, List<string>>()
        {
            {"b", ["c"]},
            {"d", ["e", "f", "g"]}
        }
    },
};

EDIT: In order to work with JSON you'd need to take a class based approach. 编辑:为了使用JSON,您需要采用基于类的方法。 It would look something like this: 它看起来像这样:

public class A
{
    public string b { get; set; }
    public List<object> d { get; set; }
}

public class RootObject
{
    public A a { get; set; }
}

And the corresponding JSON (direct translation from your example): 以及相应的JSON(示例中的直接翻译):

{
    "a": {
        "b":"c",
        "d":["e", "f", {"g":1}]
    }
}

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

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