How can I get value from IDictionary ? When I try I got "does not contain method TryGetValue" error

901 views Asked by At

I have an IDictionary object , my question is how I can read data by key? It looks easier question but there is no method (even similiar one is not found) like TryGetValue . Only Contains method looks available, but I want to get value of data by key.

Am I missing stg?

Edit: As detail

I am trying to get values from:

var browser = System.Web.HttpContext.Current.Request.Browser;

I want to get value from the IDictionary object(named Capabilities) which is defined inside System.Web.HttpContext.Current.Request.Browser:

public IDictionary Capabilities { get; set; }

When I try to read value from this object, I can not.

1

There are 1 answers

0
NetMage On

You can easily write an extension method for HttpBrowserCapabilities:

public static class CapExt {
    public static bool TryGetValue(this HttpBrowserCapabilities b, string key, out string value) => (value = (string)b[key]) != null;
}

Or for IDictionary:

public static class IDictionaryExt {
    public static bool TryGetValue(this IDictionary d, object key, out object value) => (value = d[key]) != null;
}