Pass a data structure to a function and access the fields of the data structure like an array in C# -
i have set of fields of different types, must group them data structure. have pass function , modify fields data structure indexes, array. how this? everyone.
mytype{ int a; ushort b; string c; }
and pass data structure groups these fields, can class or struct. , pass function , modify fields of instance indexes, this:
void function(ref mytype g) { g[1] = 1; <= here g.a should set 1 }
you use list of objects:
var list = new list<object>(); list.add("strings cool!"); list.add(42); // ... var objbyindex = list[1]; // 42
you lose advantages of c#'s strong-typing if this, though. suggest using class well-defined, typed properties instead, unless need generic (where don't know properties' types before-hand).
Comments
Post a Comment