添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I have a question about how to get values from my Dictionary<string, List<object>> , I tried all examples which I found in google, but still can't get something readable value.....

so, here is the code:

List<object> listValues = new List<object>();
listValues.Add(risk);
listValues.Add(validFrom);
listValues.Add(effectiveDate);
Dictionary<string, List<object>> dic = new Dictionary<string, List<object>>();
dic.Add(nameOfInsuredObject, listValues);
foreach (object value in dic.Values)
  System.Console.WriteLine(value);

I can get key from dictionary, but with getting value I am stucked now.... And here is the result of this code:

Key => testInsObj
Values => System.Collections.Generic.List`1[System.Object]

So can anyone help me with it? I am new in C#, so maybe this is easy questions for others....

Seems like you should create a custom type instead List<object> And what is the problem with getting the value? it seems fine – Gilad Green Oct 12, 2017 at 16:08 What is your expected output? i.e. What do you consider "readable"? The dictionary value is a list. If you want the values from the list, iterate through the list and print the values. – itsme86 Oct 12, 2017 at 16:08 You know that each value in dic.Values in a list of objects - List<object>? So what should a .ToString() of a List<object> be in your imagination? – Rand Random Oct 12, 2017 at 16:09 Values is showing the result of .ToString() which is often the name of the object's type. If you want something else you can compute a different string. – Tom Blodget Oct 12, 2017 at 16:17

It seems you are looking for writing values of the list this way:

foreach (var value in dic.Values)
    value.ForEach(Console.WriteLine);

In fact each element of the Dictionary is <string, List<Object>>. So, when you want to write Value part of the pair to console, you need a for loop to write each element of the List<object>.

Just for your information value.ForEach(Console.WriteLine); is equivalent to value.ForEach(x=>Console.WriteLine(x)); if it's more readable for you :) – Reza Aghaei Oct 12, 2017 at 16:20 it is also equivliant to foreach(var x in value) { Console.WriteLine(x); } if the .ForEach method is confusing you. – Scott Chamberlain Oct 12, 2017 at 16:43

It is confusing for new C# users, how to access the dictionary.

When you do a foreach on the dictionary, you get a KeyValuePair<TKey, TValue>. Now this KeyValuePair<TKey, TValue>, has 2 properties KeyValuePair.Key and KeyValuePair.Value, representing the Key and Value stored in the dictionary.

Also, the Value in your case is a List<T>, which means doing a Console.WriteLine on it will not print the whole List<T> (as some people expect), but just some reference string. You will have to "loop" over the list to print individual elements. Needless to say, depending on what you want to do with the element in the List<T>, you can use LINQ or some other common C# idiom.

foreach (var value in dic) {
    Console.WriteLine(value.Key);
    foreach (var item in value.Value)
        Console.WriteLine(item);
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.