Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

5
  • 1
    That is only one "caveat". Should also consider "lifting" of value-types and cases such as (Guid)null (it's okay to cast a null to a reference-type), among other things. Commented Jan 27, 2011 at 18:34
  • 1
    more expensive than in C/C++? in C++ the recommended way is to pass objects by value Commented Jul 8, 2013 at 20:53
  • @IonTodirel Wasn't that for memory safety reasons, rather than performance? It's always a trade-off, but passing 32 B by stack is always(TM) going to be slower than passing a 4 B reference by register. However, also note that the use of "value / reference" is a bit different in C# and C++ - when you pass a reference to an object, you're still passing by value, even though you're passing a reference (you're passing the value of the reference, not a reference to the reference, basically). It's not value semantics, but it's technically "pass-by-value". Commented Nov 23, 2015 at 13:25
  • @Luaan Copying is only one aspect of costs. The extra indirection due to pointer/reference also costs per access. In some cases the struct can even be moved and thus doesn't even need to be copied. Commented Feb 19, 2016 at 16:11
  • There is a difference between using a struct with and without the new operator. I'm not sure many people use struct in a way that doesn't use the heap. stackoverflow.com/a/54910327/458321 Commented May 23, 2024 at 14:03