Quoting via httpcache c #

On VB.NET, I can do such things to write out all the keys in the cache:

Dim oc As HttpContext = HttpContext.Current For Each c As Object In oc.Cache oc.Response.Write(c.Key.ToString()) Next 

Although .Key does not appear in Intellisense, the code works fine.

How to do the same in C #?

 HttpContext oc = HttpContext.Current; foreach (object c in oc.Cache) { oc.Response.Write(c.key.ToString()); } 

This is not like .key. Little. I'm at a loss here. Any ideas on how to access the key this way?

+6
source share
2 answers

Below code binding works fine:

 HttpContext oc = HttpContext.Current; foreach (var c in oc.Cache) { oc.Response.Write(((DictionaryEntry)c).Key.ToString()); } 

thank you for your time

+9
source

Almost to the right is K capital for Key , not lower case. C # is case sensitive.

In addition, object does not have a Key member. In C #, you can use implicit type inference with the var keyword. This will work if the underlying candidate type has a Key member:

 HttpContext oc = HttpContext.Current; foreach (var c in oc.Cache) { oc.Response.Write(c.Key.ToString()); } 

In this case, Cache does not have a Key member, so you need to dig deeper using the IDictionaryEnumerator returned by the GetEnumerator Cache method:

 HttpContext oc = HttpContext.Current; IDictionaryEnumerator en = oc.Cache.GetEnumerator(); while(en.MoveNext()) { oc.Response.Write(en.Key.ToString()); } 
+10
source

Source: https://habr.com/ru/post/901988/


All Articles