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
Post a Comment