Select Value of List of KeyValuePair

前端 未结 4 1274
滥情空心
滥情空心 2021-01-03 19:43

How can i select the value from the List of keyvaluepair based on checking the key value

List> myList = new         


        
4条回答
  •  长发绾君心
    2021-01-03 20:27

    Use Dictionary>. Then you can do

    List list = dict[5];
    

    As in:

    Dictionary> dict = new Dictionary>();
    dict[0] = ...;
    dict[1] = ...;
    dict[5] = ...;
    
    List item5 = dict[5]; // This works if dict contains a key 5.
    List item6 = null;
    
    // You might want to check whether the key is actually in the dictionary. Otherwise
    // you might get an exception
    if (dict.ContainsKey(6))
        item6 = dict[6];
    

提交回复
热议问题