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

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -