3

Are BigIntegers in C# passed by value or by reference? I am having a code in which I need to pass them by value and I think they are passed as reference personally, as the value is changed after some calls in initial calling function, not sure though if it is the case actually.

Please help :)

vinaych
  • 79
  • 1
  • 10

2 Answers2

2

Technically speaking, all objects in C# (and Java as well) are passed by value (by default).

  • For reference types, this value happens to be a reference (pointer). That's why changes inside the method affects the actual object.
  • For value types, this value is the actual value the variable holds. That's why changes inside the method doesn't affect the actual object.

BigInteger is not special in anyway, it's a struct (which is a value type).

If you want to pass an object by reference, you can use ref keyword.

See also:

You may also want to give a look at the answers for this question.

Youssef13
  • 3,836
  • 3
  • 24
  • 41
  • 1
    @OlivierRogier Nope, that's not false. That's just a common misunderstanding by lots of developers. That's why `ref` modifier exists for C#. It's to alter the default behavior from "passing by value" to "passing by reference". – Youssef13 Jan 15 '21 at 20:50
  • @OlivierRogier Please see [this docs](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/passing-reference-type-parameters). It explains passing reference types by value vs. passing them by reference. I'll also update my answer to include this link. – Youssef13 Jan 15 '21 at 20:53
  • "instances of classes are passed by reference by default" => not true. Please read my explanation again, and also check the documentation which supports my words. Also refer to @JonSkeet's answer in https://stackoverflow.com/questions/8708632/ – Youssef13 Jan 15 '21 at 21:01
1

BigInteger is a struct, so it is a value type and it is passed by value by default:

Because a struct is a value type, when you pass a struct by value to a method, the method receives and operates on a copy of the struct argument. The method has no access to the original struct in the calling method and therefore can't change it in any way. The method can change only the copy.

Passing Value-Type Parameters (C# Programming Guide)

A value-type variable contains its data directly as opposed to a reference-type variable, which contains a reference to its data. Passing a value-type variable to a method by value means passing a copy of the variable to the method. Any changes to the parameter that take place inside the method have no effect on the original data stored in the argument variable. If you want the called method to change the value of the argument, you must pass it by reference, using the ref or out keyword. You may also use the in keyword to pass a value parameter by reference to avoid the copy while guaranteeing that the value will not be changed. For simplicity, the following examples use ref.

If you want to pass such type by reference, you can use the ref keyword.

Passing Reference-Type Parameters (C# Programming Guide)

A variable of a reference type does not contain its data directly; it contains a reference to its data. When you pass a reference-type parameter by value, it is possible to change the data belonging to the referenced object, such as the value of a class member. However, you cannot change the value of the reference itself; for example, you cannot use the same reference to allocate memory for a new object and have it persist outside the method. To do that, pass the parameter using the ref or out keyword. For simplicity, the following examples use ref.

It will therefore be only the memory pointer (4 or 8 bytes on x32 or x64 systems) which is pushed into the stack before the call of a method (like an instance of a class) instead of the entire structure content (the copy of all members, for example 8x4 bytes if the struct has 8 integers, at least).

Pass c# struct by reference

Passing a struct by Reference

Pass reference by reference vs pass reference by value - C#

Passing a Reference vs. Value (Pluralsight)

Understanding C# Pass by Reference and Pass by Value (Udemy)

Passing Arguments By Value and By Reference (C# 6 for Programmers, 6th Edition)

  • 4
    The way the first sentence is worded suggests that being passed by value is a *consequence* of being a value type. That is false and misleading. The *only* thing relevant in by-value vs. by-reference is the presence of `ref`, `out`, or `in` before the type. Without those, it's by-value. What the value that's passed is... *that* is a consequence of being a value type or a reference type. – madreflection Jan 15 '21 at 20:11
  • 2
    Reference types are also passed by value. The value is the reference. – madreflection Jan 15 '21 at 20:18
  • 1
    I don't understand why you're quoting docs to me. I'm not saying anything different. Your own wording in the answer is *incorrectly paraphrasing* the docs. – madreflection Jan 15 '21 at 20:28
  • 1
    That update doesn't fix the problem. The first sentence still implies that it's only passed by value *because* it's a value type (causality contrasted from reference types). – madreflection Jan 15 '21 at 20:35