Don't know from. NET a few start to have these 2 keywords, but can really reduce some of the programming code, performance is also unknown, only record the comparison of 2 different encodings, get the same effect:
First, the use of general code:
Let's start with the first 3 model classes:
1 Public classNmenus2 {3 PublicList<mtitle> Titles {Get;Set; }4 5 PublicNmenus ()6 {7Titles =NewList<mtitle>();8 }9 }Ten One Public classMtitle A { - Public intTid {Get;Set; } - Public stringTname {Get;Set; } the Public stringTicon {Get;Set; } - - PublicList<mtitem> Items {Get;Set; } - + PublicMtitle () - { +Items =NewList<mtitem>(); A } at } - - Public classMtitem - { - Public intIID {Get;Set; } - Public stringIname {Get;Set; } in Public stringIicon {Get;Set; } - Public stringIurl {Get;Set; } to}
View Code
Then look at the code that filters and goes to JSON:
1Nmenus menus =NewNmenus ();2 foreach(Sysnavmenu titleinchtitles)3 {4 varItems = fromMinchmenuslist5 whereM.pmid = =title. MId6 Select NewMtitem {iid = m.mid, Iname = m.mtext, Iicon = M.icon, Iurl =M.murl};7 vart =Newmtitle {tid = title. MId, Tname = title. MText, Ticon = title. Icon, items = items. Tolist<mtitem>() };8 menus.titles.Add (t);9 }TenJsonmenus = jsonconvert.serializeobject (menus);
View Code
Second, use the code of VAR and dynamic:
1 varMenus =New{titles =NewList<dynamic>() };2 foreach(Sysnavmenu titleinchtitles)3 {4 varItems = fromMinchmenuslist5 whereM.pmid = =title. MId6 Select New{iid = M.mid, Iname = m.mtext, Iicon = M.icon, Iurl =M.murl};7 vart =New{tid = title. MId, Tname = title. MText, Ticon = title. Icon, items = items. Tolist<dynamic>() };8 menus.titles.Add (t);9 }TenJsonmenus = jsonconvert.serializeobject (menus);
View Code
Summary: One and two get the final Jsonmenus string is the same, the only difference is only ...
About the use of Var and dynamic