Do they? Or to speed up my program should I pass them by reference?
-
39To speed up your program you should (1) set meaningful goals, (2) test to see if you've met your goals, (3) if you haven't, use a profiling tool to find the slowest thing, and (4) optimize the slowest thing. – Eric Lippert Jun 08 '09 at 23:42
-
1See this also http://stackoverflow.com/q/4311226/344822 – prabhakaran Mar 04 '14 at 09:08
-
4@EricLippert Your comment answers the OP's real question far better than any of the posted answers. Everyone was so distracted by the "pass reference by value" technicalities that they completely overlooked the real question about performance optimization. Unfortunately the OP communicated his needs rather poorly, and the other answers will at least help the Google crowd. – Dan Bechard Sep 03 '15 at 17:44
-
1"other answers will at least help the Google crowd" - they will! :) – miki Nov 02 '16 at 14:07
4 Answers
The reference is passed by value.
Arrays in .NET are object on the heap, so you have a reference. That reference is passed by value, meaning that changes to the contents of the array will be seen by the caller, but reassigning the array won't:
void Foo(int[] data) {
data[0] = 1; // caller sees this
}
void Bar(int[] data) {
data = new int[20]; // but not this
}
If you add the ref
modifier, the reference is passed by reference - and the caller would see either change above.

- 147,647
- 23
- 218
- 272

- 1,026,079
- 266
- 2,566
- 2,900
-
34The answer is correct but I have a pet hate of people who state "the reference is passed by value". It confuses the issue. Yes, references (memory addresses) are passed by value but that was not the question. – WernerVA May 02 '15 at 13:37
-
14@WernerVA I'm interested how you think you would express it differently that remains *correct* and *unambiguous* – Marc Gravell May 02 '15 at 17:01
-
2I wouldn't express is differently, just perhaps a with a bit more detail. – WernerVA May 07 '15 at 06:48
-
This doesn't answer the speed portion of the question. It's probably obvious to you that optimizing out a reference copy is going to be completely negligible compared to the hundreds of other far more expensive operations taking place, but the OP did ask this specifically. – Dan Bechard Sep 03 '15 at 17:42
-
7@Dan the speed part is inherently linked to the "if the array is passed by value - meaning a copy of the array contents - that means an allocation and a copy of all the memory". Since it is a *reference* that is passed, there is no performance question to answer - that portion of the question is rendered moot and answered automatically. Except now it is also answered here explicitly in the comments... – Marc Gravell Sep 04 '15 at 08:14
-
-
rarely i understood a concept this easily like in this case i did, stumbled upon this somehow all of a sudden. THanks a lot for such a clarification ! – kuldeep Aug 16 '17 at 10:02
They are passed by value (as are all parameters that are neither ref nor out), but the value is a reference to the object, so they are effectively passed by reference.

- 48,267
- 11
- 78
- 120
(1) No one explicitly answered the OP's question, so here goes:
- No. Explicitly passing the array or list as a reference will not affect performance.
- What the OP feared might be happening is avoided because the function is already operating on a reference (which was passed by value). The top answer nicely explains what this means, giving an Ikea way to answer the original question.
(2) Good advice for everyone:
- Read Eric Lippert's advice on when/how to approach optimization. Premature optimization is the root of much evil.
(3) Important, not already mentioned:
- Use cases that require passing anything - values or references - by reference are rare.
- Doing so gives you extra ways to shoot yourself in the foot, which is why C# makes you use the "ref" keyword on the method call as well. Older (pre-Java) languages only made you indicate pass-by-reference on the method declaration. And this invited no end of problems. Java touts the fact that it doesn't let you do it at all.

- 11
- 3
Yes, they are passed by reference by default in C#. All objects in C# are, except for value types. To be a little bit more precise, they're passed "by reference by value"; that is, the value of the variable that you see in your methods is a reference to the original object passed. This is a small semantic point, but one that can sometimes be important.

- 38,903
- 3
- 77
- 117
-
10The reference is passed by value; which is very different to "they are passed by reference". – Marc Gravell Jun 08 '09 at 22:49
-
1This is the exact opposite of the truth. Quoting [this post from John Skeet](https://devblogs.microsoft.com/csharpfaq/how-are-parameters-passed-in-c-are-they-passed-by-reference-or-by-value/): "By default, all parameters are passed by value in C#. Parameters are only passed by reference if you explicitly include an out or ref modifier. However, you need to be aware that when the type of the parameter is a reference type, you’re passing a reference rather than an actual object". It's only the reference (the memory address) that is passed by value. – alelom Jun 04 '19 at 17:21