-4

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.

NetMage
  • 26,163
  • 3
  • 34
  • 55
Ozmen
  • 129
  • 3
  • 10
  • 1
    Use the indexer `dict[key]`? – Sweeper Dec 10 '19 at 15:26
  • `IDictionary` does not have a method `TryGetValue`. –  Dec 10 '19 at 15:26
  • 4
    _Why_ do you have a nongeneric `IDictionary` object to begin with? `TryGetValue()` is defined in the generic `IDictionary` interface. – CodeCaster Dec 10 '19 at 15:27
  • 1
    You're looking for [IDictionary](https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.idictionary-2?view=netframework-4.8) but you have [IDictionary](https://learn.microsoft.com/en-us/dotnet/api/system.collections.idictionary?view=netframework-4.8) – A Friend Dec 10 '19 at 15:27
  • Look at the docs of `IDictionary`. There exists an indexer, which you can use exacty as you intend: `dict[key]`. – MakePeaceGreatAgain Dec 10 '19 at 15:30
  • I had already tried accessing by index but does not work, it says "can not apply indexing with to an expression of type ICollection" – Ozmen Dec 10 '19 at 16:17
  • You said you had an `IDictionary`. Now its an `ICollection`? –  Dec 10 '19 at 16:20
  • @Amy no , I have IDictionary Capabilities { get; set; } but adviced to access by index , I replied I had already tired this and got error like previous comment. ICollection Keys { get; } are included in IDictonary. – Ozmen Dec 10 '19 at 16:25
  • 1
    Yes, `IDictionary` has a property `Keys`, but you were advised to use `Capabilities["key"]`. It is unclear what the issue is now. –  Dec 10 '19 at 16:26
  • Ohh thanks. I must have been experienced so busy day, how I missed this stuff:) But I would want to use TryGetValue , but it looks impossible – Ozmen Dec 10 '19 at 17:34
  • @Ozmen [See here](https://stackoverflow.com/questions/59270514/how-can-i-get-value-from-idictionary-when-i-try-i-got-does-not-contain-method#comment104748204_59270514) –  Dec 10 '19 at 18:00
  • I don't think `Capabilities` is intended for public use, `HttpBrowserCapabilities` implements an indexer property directly. – NetMage Dec 10 '19 at 22:54

1 Answers1

0

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;
}
NetMage
  • 26,163
  • 3
  • 34
  • 55