c# - How to make foreach loop with custom class and List<> in class -


if have class:

    public class cacheclass     {         public string userid { get; set; }         public list<string> tabid { get; set; }         public list<string> state { get; set; }         public list<string> canadmin { get; set; }     } 

then add value class , add cache. assign var type variable cache value:

    var k = system.web.httpcontext.current.cache[objuserinfo.userid.tostring()]; 

so, how can foreach loop var k , value?

as see k object (hover on var), since cache dictionary isn't typed. compiler doesn't know actual type cacheclass. step 1 cast it. prefer use as since won't throw exception if casting fails:

var k = system.web.httpcontext.current.cache[objuserinfo.userid.tostring()] cacheclass; 

using as require to null-check make sure cast went okay:

if (k != null) {     foreach (string x in k.state)     {  } } 

Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -