I am wondering, when does it become more efficient to pass by value in C#? I was reading this article:
http://msdn.microsoft.com/en-us/library/4d43ts61%28v=vs.90%29.aspx
The article suggests that passing by reference actually generates overhead when it transferrs the value type to the heap. My C/C++ classes swore by passing by reference as a good coding practice, and I have passed even primitive values by reference for a while now. I am in C# now, so I guess it is possible that it does not funciton the same, but I always thought that passing a pointer to a value was more efficient than copying the value of a variable to a new local variable in memory. Is there a certain number of bytes a data type has to be to become more efficient to pass by reference?
overwraith
83
Newbie Poster
Recommended Answers
Jump to PostIt depends how much data you're copying really. It's nearly always cheaper to pass a class by reference.
Reference types are created on the heap, so there is no penalty of passing a reference type by reference (classes are reference types).
However, to pass a value type (such as a …
Jump to PostMy C/C++ classes swore by passing by reference as a good coding practice, and I have passed even primitive values by reference for a while now.
Even in C++ it's a tradeoff. The size of a reference versus the size of the object being passed relative to the …
All 6 Replies
Ketsuekiame
860
Master Poster
Featured Poster
deceptikon
1,790
Code Sniper
Team Colleague
Featured Poster
overwraith
83
Newbie Poster
Ketsuekiame
860
Master Poster
Featured Poster
deceptikon
1,790
Code Sniper
Team Colleague
Featured Poster
Ketsuekiame
commented:
+1 This kind of micro-management is very rarely required.
+11
overwraith
83
Newbie Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.