Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the best way to pass big data into C# functions?

Tags:

c#

Basically, I have a very large byte[] and a class of helper functions.

Am I right in thinking if I call Helpers.HelperFunc(mybigbytearray), mybigbytearray will be duplicated in memory?

If so, what's the best way to give a big variable to a function (pointers look good but is making the helper functions unsafe wise? Would the garbage collector still work?)

like image 274
James Avatar asked Feb 16 '23 12:02

James


1 Answers

Arrays, like other objects in C#, are passed by reference, thus no data inside the array will be duplicated; the function you pass the array to will have a reference to the original array.


From Arrays as Objects (C# Programming Guide)

In C#, arrays are actually objects, and not just addressable regions of contiguous memory as in C and C++. System.Array is the abstract base type of all array types.

and Passing Arrays as Arguments (C# Programming Guide)

Arrays can be passed as arguments to method parameters. Because arrays are reference types, the method can change the value of the elements.

like image 200
Elle Avatar answered Mar 02 '23 00:03

Elle