c# - How to get the field names of a list inside foreach? -
i have following class:
public class listofvariablestosave : list<list<int>> { public list<int> controldb { get; set; } public list<int> interacdb { get; set; } public listofvariablestosave() { controldb = new list<int> { 1, 2, 3 }; interacdb = new list<int> { 21, 22, 23 }; add(controldb); add(interacdb); } }
and following code write content text file:
listofvariablestosave mylistofvariablestosave = new listofvariablestosave(); streamwriter myfile = new streamwriter("filename.txt"); foreach (list<int> db in mylistofvariablestosave) { foreach (int vartosave in db) { myfile.writeline(vartosave); } } myfile.close();
what is:
1 2 3 21 22 23
what is:
controldb 1 2 3 interacdb 21 22 23
is possible perhaps adding single line of code after first foreach
?
i think doing this:
public class listofvariablestosave : list<listofthings<int>> { public listofthings<int> controldb { get; set; } public listofthings<int> interacdb { get; set; } public listofvariablestosave() { controldb = new listofthings<int>() { 1, 2, 3 }; controldb.name = "controldb"; interacdb = new listofthings<int>() { 21, 22, 23 }; interacdb.name = "interacdb"; add(controldb); add(interacdb); } } public class listofthings<t> : list<t> { public string name { get; set; } public listofthings() : base() { } }
instead of listofvariablestosave
being derived list<list<int>>
instead create class derives list<int>
, adds name property.
you can iterate this:
var lists = new listofvariablestosave(); foreach (var list in lists) { console.writeline(list.name); foreach (var in list) { console.writeline(i); } }
Comments
Post a Comment