Scannig through a program a simple program at the moment but need to understand "pass by rederence" to coomplete it... Could someone explain what this means??
This is a discussion on Pass by reference question.. within the C# Programming forums, part of the General Programming Boards category; Scannig through a program a simple program at the moment but need to understand "pass by rederence" to coomplete it... ...
Scannig through a program a simple program at the moment but need to understand "pass by rederence" to coomplete it... Could someone explain what this means??
Lets say you have a text on a piece of paper.
"pass by value" means you make a copy and give it to me. I can write notes on my piece of paper, but for obvious reasons they won't magically appear on your orginal sheet.
"pass by reference" means you actually give that piece of paper to me. Anything I write on it, will still be on it when I give it back. As you don't need to make a copy, it's also faster.
All reference types are "pass by reference" in C# while all value types are automatically "pass by value" so there is no syntax I could explain.
hth
-nv
She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."
When in doubt, read the FAQ.
Then ask a smart question.
I'd like to make a small clarification because the following is an usual source of confusion among beginners to the language:
All reference types in C# (all types not derived from System.ValueType) are passed by reference, just as nvoigt points out. However they still can be passed by reference or by value with different results.
foo is a class type. As such it is a reference-type (not derived from System.ValueType, but from System.Object) and will always be passed by reference between caller and callee. So if I'm to write the following code, bar will indeed change:Code:class foo { public int value; public foo(int val) { value = val; } }
If I change my code to pass by reference, the same thing will happen.Code:void Main() { foo bar = new foo(13); Console.WriteLine(bar.value); // will print 13 ChangeBarByVal(bar); Console.WriteLine(bar.value); // will print 31 } static void ChangeBarByVal(foo f) { // foo being passed by-value f.value = 31; }
The question thus is, why would I want to pass by ref, something that is always passed by ref? The real distinction between passing by val and by ref, when it comes to ref types is not in the ability to change the object state. This will always be guaranteed. But in the ability to reassign the object.Code:void Main() { foo bar = new foo(13); Console.WriteLine(bar.value); // will print 13 ChangeBarByRef(ref bar); Console.WriteLine(bar.value); // will print 31 } static void ChangeBarByRef(ref foo f) { // foo being passed by-ref f.value = 31; }
As you can see, passing by value won't allow you to reassign the object (change the instance to where your reference is pointing). But passing by ref will.Code:void Main() { foo bar = new foo(13); Console.WriteLine(bar.value); // will print 13 ChangeBarByValue(bar); Console.WriteLine(bar.value); // will still print 13, not 31 ChangeBarByRef(ref bar); Console.WriteLine(bar.value); // will print 65 } static void ChangeBarByValue(foo f) { // foo being passed by-value f = new foo(31); } static void ChangeBarByRef(ref foo f) { // foo being passed by-ref f = new foo(65); }
Originally Posted by brewbuck:
Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.