簡體   English   中英

當有更多值時,如何從字典中獲取值

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

我在互聯網上搜索過如何制作具有多個值的字典,我找到了這個

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; }
}

然后

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

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

我的問題是,如何從該字典中獲得特定值? 例如,我如何獲得 int 42?

您需要聲明一個 MyType 類型的對象來存儲字典的值。

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

如果你只想存儲一堆 MyType 對象,你可以使用 List ,那么你不必顯式添加索引器,但你仍然可以通過索引訪問它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM