C# Parameter
-
Hi, Can anyone tell me how the normal parameter in C# are passed? is it by Ref? or just value? Cuz i'm getting a bit problem with my code. I have this code that calculates statistics and for that i need to sort() my array. but this method also (somehow) is being applied to the array that i passed into the function... for i.e.
private int iAnswer(ArrayList arData) { arData.Sort(); } text1.Text = iAnswer(arNum).ToString();
So what i'm trying to say is that when i do arData.Sort() it also sorts arNum somehow??!!- Stop thinking in terms of limitations and start thinking in terms of possibilities -
-
Hi, Can anyone tell me how the normal parameter in C# are passed? is it by Ref? or just value? Cuz i'm getting a bit problem with my code. I have this code that calculates statistics and for that i need to sort() my array. but this method also (somehow) is being applied to the array that i passed into the function... for i.e.
private int iAnswer(ArrayList arData) { arData.Sort(); } text1.Text = iAnswer(arNum).ToString();
So what i'm trying to say is that when i do arData.Sort() it also sorts arNum somehow??!!- Stop thinking in terms of limitations and start thinking in terms of possibilities -
C# parameters are passed by value. But there are some sutle things here. C# has reference-base objects and value-based objects. For the reference-based object, you can think it as an object holding an address. So, when you pass them to a function, only the address will be copied. For your code, the last line passed arNum to iAnswer. iAnswer copy the address holded by arNum to it's own parameter, arData. When you sort arData, the real object that is pointed by arData and arNum will be changed.
-
Hi, Can anyone tell me how the normal parameter in C# are passed? is it by Ref? or just value? Cuz i'm getting a bit problem with my code. I have this code that calculates statistics and for that i need to sort() my array. but this method also (somehow) is being applied to the array that i passed into the function... for i.e.
private int iAnswer(ArrayList arData) { arData.Sort(); } text1.Text = iAnswer(arNum).ToString();
So what i'm trying to say is that when i do arData.Sort() it also sorts arNum somehow??!!- Stop thinking in terms of limitations and start thinking in terms of possibilities -
The ArrayList is a object, so it's allocated to the heap (a reference type). So in your case it is by ref. Some further reading: http://msdn2.microsoft.com/en-us/library/3ewxz6et(VS.71).aspx[^]
-
Hi, Can anyone tell me how the normal parameter in C# are passed? is it by Ref? or just value? Cuz i'm getting a bit problem with my code. I have this code that calculates statistics and for that i need to sort() my array. but this method also (somehow) is being applied to the array that i passed into the function... for i.e.
private int iAnswer(ArrayList arData) { arData.Sort(); } text1.Text = iAnswer(arNum).ToString();
So what i'm trying to say is that when i do arData.Sort() it also sorts arNum somehow??!!- Stop thinking in terms of limitations and start thinking in terms of possibilities -
Hi, if you are familiar with older languages such as C, it is very similar: 1. simple things (int, double) get copied (pass by value); 2. for complex things (strings, class instances) a pointer is passed (so there is no object copied, it is the same object you get). Whether you call this by value or by ref however is a bit confusing (you can optionally add a ref keyword to increase the level of indirection by one) :)
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.
-
Hi, Can anyone tell me how the normal parameter in C# are passed? is it by Ref? or just value? Cuz i'm getting a bit problem with my code. I have this code that calculates statistics and for that i need to sort() my array. but this method also (somehow) is being applied to the array that i passed into the function... for i.e.
private int iAnswer(ArrayList arData) { arData.Sort(); } text1.Text = iAnswer(arNum).ToString();
So what i'm trying to say is that when i do arData.Sort() it also sorts arNum somehow??!!- Stop thinking in terms of limitations and start thinking in terms of possibilities -
-
The ArrayList is a object, so it's allocated to the heap (a reference type). So in your case it is by ref. Some further reading: http://msdn2.microsoft.com/en-us/library/3ewxz6et(VS.71).aspx[^]
Bert delaVega wrote:
The ArrayList is a object, so it's allocated to the heap (a reference type). So in your case it is by ref.
That is not correct. It's a reference that is passed as a parameter, but it's not passed by reference. The reference itself is copied and sent as a parameter, it's not a reference to the reference that is sent as a parameter.
Despite everything, the person most likely to be fooling you next is yourself.