Home > Net >  How to create SortedDictionary ValueCollection type?
How to create SortedDictionary ValueCollection type?

Time:06-28

I am trying to create object with the following type:

System.Collections.Generic.SortedDictionary`2 ValueCollection

When I run the fllwing command:

Console.WriteLine(typeof(SortedDictionary<string, object>));

I am getting the following output:

System.Collections.Generic.SortedDictionary`2

How can I create an object that

typeof(TheType) == System.Collections.Generic.SortedDictionary`2 ValueCollection

CodePudding user response:

the "typeof(SortedDictionary<string, object>)" will return the path in the assembly where the SortedDictionary é created

you can SortedDictionary.Add("test", new object[]);

do a foreach in it

or get the values in it

"typeof(TheType) == System.Collections.Generic.SortedDictionary`2 ValueCollection"

if you want to compare you can use

typeof(TheType) == typeof(SortedDictionary.key) typeof(TheType) == typeof(SortedDictionary.value)

CodePudding user response:

The SortedDictionary<TKey,TValue>.ValueCollection Class is a nested class. SortedDictionary<TKey,TValue> acts like a namespace for this ValueCollection:

Console.WriteLine(typeof(SortedDictionary<string, object>.ValueCollection));

sortedDictionary = new SortedDictionary<string, object>();
var valueCollection =
    new SortedDictionary<string, object>.ValueCollection(sortedDictionary);

However the SortedDictionary<TKey,TValue> automatically creates such a ValueCollection internally. So, you will not need to create it yourself. It is returned by the Values property of the dictionary.

  • Related