vinaych
vinaych

Reputation: 79

How are BigInteger passed in functions

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 :)

Upvotes: 3

Views: 463

Answers (2)

user12031933
user12031933

Reputation:

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)

Upvotes: 1

Youssef13
Youssef13

Reputation: 4993

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.

Upvotes: 2

Related Questions