dictionary get key c# code example

Example 1: c# dictionary get value by key

Dictionary<string, string> dict = new Dictionary<string, string>();
 dict.Add("UserID", "test");
 string userIDFromDictionaryByKey = dict["UserID"];

Example 2: dictionary c# get key by value

var myKey = types.FirstOrDefault(x => x.Value == "one").Key;

Example 3: get key value from object c#

Type myType = myObject.GetType();
IList<PropertyInfo> props = new List<PropertyInfo>(myType.GetProperties());

foreach (PropertyInfo prop in props)
{
    object propValue = prop.GetValue(myObject, null);

    // Do something with propValue
}