Leon Barkan Leon Barkan - 1 year ago 134
C# Question

How to get key object from Dictionary by key

In my application, I have a dictionary with key/value objects

Dictionary<TObject,VObject> list

To check if I have the object, I'm using a special instance of
and checking if it exists in the list.

After i get
, I need to get the original Key object (

VObject result = list[TObject]; // will return me the VObject

The original
(the key), has more values that have been initialized on the object creation and I need to do a test on them.

How can i do this?

Answer Source

The dictionary object contains Keys property, please see https://msdn.microsoft.com/en-us/library/yt2fy5zk(v=vs.110).aspx for more information.

You can run queries against this property to select the key you are looking for.

Useage with linq;

var foundKey = dictionaryObject.Keys.FirstOrDefault( * Linq Expression* );
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download