In C# (and presumably in VB.NET) there are three ways to pass constant-value arrays to a function, namely:
byte[] buffer = {0};
someFunction(buffer);
byte[] buffer = new byte[] {0};
someFunction(buffer);
someFunction(new byte[] {0});
Whereas simple typecasting an array declarator is invalid syntax:
someFunction((byte[]) {0});
Question:
What are the performance difference between the three working methods – in terms of CPU usage, memory allocation, and overall program size? Does the use of the new keyword have any effect on RAM usage or allocation, especially in cases where the declared variable falls out of scope immedately after the function call?
is simply a syntactic shortcut for
and
is simply a syntactic shortcut for
So, as the others have concluded, you are not in fact passing the array to the method in different ways – you are always performing the exact same operation: All three pieces of code allocate local stack storage for a reference, all three perform a heap allocation via
newand initialise the array memory with the provided value. Lastly, all pass the array reference to the method by value.