arrays - Passing by reference to n-th element in C# -
in c, if have array, can pass reference function. can use simple addition of (n-1) pass reference starting n-th element of array this:
char *strarr[5]; char *str1 = "i want that!\n"; char *str2 = "i want this!\n"; char *str3 = "i want those!\n"; char *str4 = "i want these!\n"; char *str5 = "i want them!\n"; strarr[0] = str1; strarr[1] = str2; strarr[2] = str3; strarr[3] = str4; strarr[4] = str5; printpartially(strarr + 1, 4); //we can pass start printing 2nd element .... void printpartially(char** strarrpart, char size){ int i; (i = 0; < size; ++i) printf(strarrpart[i]); } resulting in these:
i want this! want those! want these! want them! process returned 0 (0x0) execution time : 0.006 s press key continue. in c#, can pass reference object ref (or, out). object includes array, whole array (or @ least, how suppose works). how pass reference n-th element of array such internal function, there string[] elements 1 less original string[] without need create new array?
must use unsafe? looking solution (if possible) without unsafe
edit:
i understand pass array in c# without ref keyword. perhaps question sounds quite misleading mentioning ref when talk array. point why put ref there, should rather put way: ref keyword can used, say, pass reference n-th element of array as c other passing reference object (without mentioning n-th element or alike)? apology misunderstanding occurs question's phrasing.
edit:
you won't able do in c in safe code.
a c# array (i.e. string[]) derived abstract type array. not simple memory block in c.
so can't send 1 of it's element's reference , start iterate there.
but there solutions give same taste of course (without unsafe):
like:
- as @chris mentioned can use
arraysegment<t>. - as
arrayienumerable<t>can use.skip, send returned value. (but giveienumerable<t>instead ofarray). allow iterate. - etc...
Comments
Post a Comment