C# array parameter reference [duplicate] - c#

This question already has answers here:
Passing Objects By Reference or Value in C#
(9 answers)
Closed 5 years ago.
I have a C# code as follows:
int[] A = new int[] {1, 2, 3};
fun(A);
// A at this point still says 1, 2, 3.
void fun(int[] A)
{
int[] B = new int[] {4, 5, 6};
A = B;
}
I thought all arrays are passed by reference in C#. Shouldn't A after calling fun() reflect 4, 5, 6?

The array is passed by a reference, you can see this by doing A[0] = 7; from inside another method.
That reference (held by the outer variable A), however is passed by value to the function. The reference is copied and a new variable is created and passed to the function. The variable outside the function is not affected by the reassignment to the parameter variable A inside the function.
To update the original variable you need to use the ref keyword so the parameter inside the function represents the same object as outside of the function.
int[] A = new int[] {1, 2, 3};
fun2(A);
// A at this point says 7, 2, 3.
fun(ref A);
// A at this point says 4, 5, 6.
void fun2(int[] a)
{
a[0] = 7;
}
void fun(ref int[] a)
{
int[] B = new int[] {4, 5, 6};
a = B;
}

I thought all arrays are passed by reference in C#
Actually (the reference of the original array object is passed by value) which is the usual behavior in case of reference types in C#.
Your understanding is partially correct, the reference is passed but is passed by value which means a new reference gets created which is pointing to the original array object A.
The fun(int[] A) has it's own copy of reference which is pointing to the array object which contains 1,2,3 and in the fun you create a new array object B and you are just assigning the reference of new one to your local method reference variable which of-course will not have any impact on the original A object which was passed as input to the fun.
You would need to pass it by reference if you want to reflect the changes made to A in fun to be reflected back to the original array object.
You can update the array items without passing by reference which is explained well in Scott Chamberlain's answer
Hope it Helps!

Related

Array copy semantics in C# [duplicate]

This question already has answers here:
Reference type still needs pass by ref?
(6 answers)
Closed 1 year ago.
I'm trying to understand exactly what's happening in this code below:
static void someMethod(int[] a) {
int[] b = new int[5];
a = b;
}
static void Main(string[] args) {
int[] arr = new int[10];
someMethod(arr);
Console.WriteLine(arr.Length);
}
The thing I can't explain is why the Console.Writeline() prints 10 and not 5. Arrays in C# are reference types, so I would've thought the line a = b sets the variable a to point to an array of int[5]. Yet the Console.Writeline() prints 10. I'm at a loss to explain this value-type-like behavior of arrays. Can someone explain this?
Think of a, b, and arr like boxes you can store references in.
You store a reference in arr. You pass the reference, but not the box itself, to someMethod, which sticks that reference in its own box, a.
You then create a new object and store a reference to it in b. Then you take the reference that is inside b, and stick it inside a, replacing the reference to the passed-in array inside that box.
But this does not change the value stored in arr, which contains the originally referenced object.
You can modify the referenced object, however:
static void someMethod(int[] a)
{
a[2] = 10;
a[3] = 7;
}
static void Main(string[] args)
{
int[] arr = new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
someMethod(arr);
Console.WriteLine(arr[2]);
}
You will find that the array has been modified.
Using the ref keyword, as suggested in the comments, basically tells it to pass the "box" itself. See the documentation for more information.
The assignment is writing an int pointer, not copying the array definition or its items. After assigning that pointer, the size of the array does not change. Even though it is now pointing to just 5 ints.

In C# what is the benefit of passing value by ref?

I am trying to understand what is the benefit of passing method params by ref in c# instead of passing parameters by value.
I have a list of custom objects (around 50k) and I need to run some operation on its properties. I have written a calculation class which accepts that list of 50K elements and returns the value. I am wondering if I pass the parameter by the ref, is it going to save my system memory in runtime as I am passing the reference and not passing a copy of the 50k list? How does .NET maintains this actually?
main(){
var itemList={}//list containing 50k items
var result=Calculate(itemList);// it passes a copy of the array
var resultByRef=Calculate(ref itemList); //**it passes address of result variable, is it going to take less memory in runtime??**
}
private int Calculate(List<CustomClass> itemList){
//do some calculation
return result;
}
private int CalculateByRef(ref List<CustomClass> itemList){
//do some calculation
return result;
}
Looks like you are coming from C++ background like me.
In C# every object is passed around but its reference all the time which means no matter how large the object is, you always pass its reference to methods.
The only difference the ref keyword makes is give you ability to change that reference itself. Let's understand with an example:
static void callByRef(ref byte[] buff)
{
buff[0] = 10;
buff = new byte[5];
}
static void callNormally(byte[] buff)
{
buff[0] = 10;
buff = new byte[5];
}
static void Main(string[] args)
{
byte[] param = new byte[5];
param[0] = 5;
Console.WriteLine("Original param.Hash: " + param.GetHashCode());
callNormally(param);
Console.WriteLine("param[0]: " + param[0]);
Console.WriteLine("param.Hash: " + param.GetHashCode());
callByRef(ref param);
Console.WriteLine("param[0]: " + param[0]);
Console.WriteLine("param.Hash: " + param.GetHashCode());
return;
}
The output is as follows:
Origenal param.Hash: 58225482
param[0]: 10
param.Hash: 58225482
param[0]: 0
param.Hash: 54267293
In a normal call too, you can change the contents inside the object but in case of ref call the object itself can be changed.
In your case you are only worried about memory replication in case of passing large data as parameter to a method which happens in case of C++. In C# that is not the case.
Passing by reference will not help you here.
This is because passing by value for a reference type list a list or array still passes the reference. The difference is when you pass by value, you pass a copy (value) of the reference, but it's only the reference that is copied. When you pass by reference you pass the original variable.
Copying a mere 20 byte reference isn't meaningfully different than what you need to do to make actual reference passing work, and so there's no performance advantage. Passing by reference is only useful if you need to change the variable itself: for example, assign a completely new List object to it.
//pass itemList by value
private int Calculate(List<CustomClass> itemList)
{
itemList[0] = new CustomClass(); // this still works!
//The List reference that was passed to this method still refers
// to the *same List object* in memory, and therefore if we update
// the item at position [0] here it will still be changed after the
// method returns.
// But this does NOT change the original after the method ends,
// because itemList is a different variable and we just changed
// it to refer to a whole new object.
itemList = new List<CustomClass>();
// If we had instead passed this by reference, then the calling code
// would also see the brand new list.
}
Let's work through the following example.
static void Main(string[] args)
{
var myLocalList = new List<int> { 1, 2, 3 }; // 1
myLocalList.ForEach(x => Console.WriteLine(x)); // 2
Calculate1(myLocalList); // 3
myLocalList.ForEach(x => Console.WriteLine(x)); // 5
Calculate2(ref myLocalList); // 6
myLocalList.ForEach(x => Console.WriteLine(x)); // 8
}
private void Calculate1(List<int> list)
{
list = new List<int> { 4, 5, 6 }; // 4
}
private void Calculate2(ref List<int> list)
{
list = new List<int> { 7, 8, 9 }; // 7
}
Step 1 creates a local list of integers initialized with values 1, 2, and 3.
Step 2 prints the list. The console output shows 1, 2, and 3 on separate lines.
Step 3 calls Calculate1 with the local list as a parameter input.
Step 4 assigns the list variable a new list of integers with values 4, 5, and 6.
Step 5 prints the list. The console output shows 1, 2, and 3 on separate lines, same as Step 2.
Step 6 calls Calculate2 with the local list as a ref parameter input.
Step 7 assigns the list variable a new list of integers with values 7, 8, and 9.
Step 8 prints the list. This time, the console output shows 7, 8, and 9 on separate lines.
When myLocalList is passed to Calculate1, the list is not copied. To be absolutely clear, what I mean specifically is that the contents of myLocalList are NOT copied to the list parameter. What is copied, however, is the reference to myLocalList. In other words, the reference to myLocalList is copied by value to the list parameter. When step 4 sets list to the new 4-5-6 list, the copied reference (i.e., list) is modified, not the original reference (i.e. myLocalList).
That changes with Calculate2. In this case, the reference to myLocalList is passed by reference to the list parameter. This effectively turns list into an alias for myLocalList, meaning that when step 7 sets list to the new 7-8-9 list, the original reference (i.e., myLocalList) is modified. That's why the output changes in step 8.
...is it going to save my system memory in runtime as I am passing the
reference and not passing a copy of the 50k list?
No. Neither the Calculate nor the CalculateByRef methods receive deep copies of itemList, so performance is not impacted in the way you suggest. Passing the parameter using the ref keyword in CalculateByRef simply allows you to modify the value of the itemList variable in Main from inside CalculateByRef.
Just based on what you've shown, it doesn't sound like you need the ref keyword in this case.
HTH

What is the difference between these two methods - pass by ref

What is the difference between the code below? Both methods achieve the same output of removing 4 from the list.
static void Main(string[] args)
{
var list = new List<int> {1, 2, 3, 4, 5, 6, 7, 8, 9};
ModifyList(ref list);
Console.WriteLine(string.Join(", ", list));
list.Add(4);
ModifyList(list);
Console.WriteLine(string.Join(", ", list));
Console.ReadLine();
}
public static void ModifyList(ref List<int> list)
{
list.Remove(4);
}
public static void ModifyList(List<int> list)
{
list.Remove(4);
}
In this case, both do exactly the same thing. The difference between passing the argument in by ref is that if you assign to the variable itself (eg. list = new List<int>();, it will update the reference of the caller to point to the new list as well.
See Passing Reference-Type Parameters (C# Programming Guide) for more info.
In this case the only difference is under the hood and not easily noticeable, the difference is that in the ModifyList(ref List<int> list) the list object is passed by reference and so the original reference (managed pointer) is the same as the original object passed, while in the second case the reference is copied to another reference so the list argument is basically a reference to another reference that point to the real object .
The result you get is the same, because in C# with safe reference is totally transparent to you but if you would have used C or C++ with raw pointers you would have noticed that in the second case you would had to dereference the pointer two times to access the object ...

Pointer to a dictionary<> in c#

I have a class, which its constructor is below
public GroupsForm(ref Dictionary<string, List<string>> groupsList)
I want to use the groupsList in other functions as well so I declared a private member in the class:
private Dictionary<string, List<string>> _groupsList;
But the problem is when I do _groupsList = groupsList; it makes a copy of the groupsList and changes made to _groupsList doesn't change groupsList, as I think it makes a deep copy by default. What I want is that it should point to that list.
In C++, this can easily be done via pointers, but can anyone tell me how can I do this in C#?
Ahmed has posted this image:
There are value types and reference types in C#. A dictionary is a reference type so whenever assigning a variable that holds a reference type to another, the reference is copied, which means that both variables will be holding the same data. So, if you change the data, you should expect it to change on both variables:
List<int> list1 = new List<int>() { 1, 2, 3 };
List<int> list2 = list1;
list2.Add(10);
list1.ForEach(x => Console.WriteLine(x)); // should print 1, 2, 3 and 10.
However, if you reassign the variable, then you are not changing the data:
List<int> list1 = new List<int>() { 1, 2, 3 };
List<int> list2 = list1;
list2 = new List<int>() { 10, 11, 12 };
list1.ForEach(x => Console.WriteLine(x)); // should print 1, 2, 3 only
One thing that caught my attention in your code though was that the constructor is expecting a ref. This means that you are passing the reference itself by reference, which in C++ would be something like a double pointer (type**). You'd do this if, in the snippet above, you want to have this effect:
void MethodA(ref List<int> list)
{
list = new List<int>() { 10, 11, 12 };
}
// ...
List<int> list1 = new List<int>() { 1, 2, 3 };
MethodA(ref list1);
list1.ForEach(x => Console.WriteLine(x)); // should print 10, 11, 12
One more thing -- AFAIK C# doesn't implement deep copies in any of it classes. You have to do it yourself.
In your InitGroups method, you've got an assignment statement to _groupsList - it's this new list that contains 3 items. You could change InitGroups to do something like:
var newGroups = (Dictionary<string, List<string>>)ser.ReadObject(reader,true);
foreach(var kvp in newGroups)
{
_groupsList.Add(kvp.Key,kvp.Value);
}
And then you'll still be working with the same Dictionary object.
You don't need the ref in your method signature. Objects are passed by reference anyway.
And no, there is no copying going on.
But the problem is when I do _groupsList = groupsList; it makes a copy of the groupsList
No, it does not. It copies a pointer. List is a reference type.
as I think it makes a deep copy by default
Think again. Your conclusion is wrong.
List<T> is a reference type, so all variables are pointers. Beginner C#, first week, first day, first hour. Go back and read the introduction then look for the error in the rest of your code you do not show us.
You are wrong on several points.
C# references are totally different from C++.
In short, you should not use ref here because it's for propagating assignments made to parameter to the calling code which is not what you mean.
You are wrong assuming there's any kind of copying involved here.
groupsList and _groups ist are two references pointing to the same object in memory.
You'll need to read more about reference and value types in C#.

Declaring a const double[] in C#? [duplicate]

This question already has answers here:
Why does C# limit the set of types that can be declared as const?
(6 answers)
Closed 9 years ago.
I have several constants that I use, and my plan was to put them in a const array of doubles, however the compiler won't let me.
I have tried declaring it this way:
const double[] arr = {1, 2, 3, 4, 5, 6, 73, 8, 9 };
Then I settled on declaring it as static readonly:
static readonly double[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9};
However the question remains. Why won't compiler let me declare an array of const values? Or will it, and I just don't know how?
This is probably because
static const double[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9};
is in fact the same as saying
static const double[] arr = new double[]{ 1, 2, 3, 4, 5, 6, 7, 8, 9};
A value assigned to a const has to be... const. Every reference type is not constant, and an array is a reference type.
The solution, my research showed, was using a static readonly. Or, in your case, with a fixed number of doubles, give everything an individual identifier.
Edit(2):
A little sidenode, every type can be used const, but the value assigned to it must be const. For reference types, the only thing you can assign is null:
static const double[] arr = null;
But this is completely useless. Strings are the exception, these are also the only reference type which can be used for attribute arguments.
From MSDN (http://msdn.microsoft.com/en-us/library/ms228606.aspx)
A constant-expression is an expression
that can be fully evaluated at
compile-time. Because the only way to
create a non-null value of a
reference-type [an array] is to apply the new
operator, and because the new operator
is not permitted in a
constant-expression, the only possible
value for constants of reference-types
other than string is null.
There is no way to have a const array in C#. You need to use indexers, properties, etc to ensure the contents of the array are not modified. You may need to re-evaluate the public side of your class.
Just to point out though... Static readonly -IS NOT CONST-
This is perfectly valid and not what you were wanting:
class TestClass
{
public static readonly string[] q = { "q", "w", "e" };
}
class Program
{
static void Main( string[] args )
{
TestClass.q[ 0 ] = "I am not const";
Console.WriteLine( TestClass.q[ 0 ] );
}
}
You will need to find other ways to protect your array.
I don't know why you needed to make it either constant or readonly. If you really want to make the whole array immutable, then a simple constant/readonly keyword will not help you, and what's worse is, it might also divert you to the wrong way.
For any non-immutable reference types, make them readonly only means you can never re-assign the variable itself, but the content is still changeable. See below example:
readonly double[] a = new double[]{1, 2, 3};
...
a = new double[] {2,3}; // this won't compile;
a[1] = 4; // this will compile, run and result the array to {1, 4, 3}
Depending on your context, there might be some solutions, one of them is, if what you really need is a list of double, List a = new List() {1,2,3,4,5}.AsReadOnly(); will give you a content-readonly list of double.
The problem is that you're declaring a constant array of double, not an array of constant doubles. I don't think there is a way to have an array of constants due to the way arrays work in C#.
The compiler error tells you exactly why you can't do it:
'arr' is of type 'double[]'.
A const field of a reference type other than string can only be initialized with null.

Categories