简体   繁体   English

当有更多值时,如何从字典中获取值

[英]How do i get value from dictionary when there are more values

I've searched on the internet for how to make dictionary with multiple values and ive found this我在互联网上搜索过如何制作具有多个值的字典,我找到了这个

class MyType
{
public string SomeVal1{ get; set; }
public string SomeVal2{ get; set; }
public bool SomeVal3{ get; set; }
public bool SomeVal4{ get; set; }
public int SomeVal5{ get; set; }
public bool SomeVal6{ get; set; }
}

then然后

var someDictionary = new Dictionary<int, MyType>();

and

someDictionary.Add( 1, 
                    new MyType{
                        SomeVal1 = "foo",
                        SomeVal2 = "bar",
                        SomeVal3 = true,
                        SomeVal4 = false,
                        SomeVal5 = 42,
                        SomeVal6 = true
                    });

My question is, how do i get specific value from that dictionary?我的问题是,如何从该字典中获得特定值? For example, how do i get int 42?例如,我如何获得 int 42?

You need to declare an object of your type MyType to store the value of the dictionary.您需要声明一个 MyType 类型的对象来存储字典的值。

    MyType _myType = someDictionary[1]
    _myType.SomeVal1 will be equal to "foo"

If you just want to store a bunch of MyType objects, you can use List then you don't have to explicitly add indexers, but you can still access it by an index.如果你只想存储一堆 MyType 对象,你可以使用 List ,那么你不必显式添加索引器,但你仍然可以通过索引访问它。

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

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