Click here to Skip to main content
15,887,083 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am confused on what is the correct way of explaining pass by Reference. I have come accross both forms of explanations on different sites.

But which of the two is correct in terms of correctly explaining what is pass by Reference in C#?:

1st description - When an argument(of Value type of Reference type) is passed by Reference "a reference to a variable" is passed.

2nd desciption- When an argument of value type is passed by Reference, "a reference to the value" is passed. And when an argument of Reference type is passed by reference, "a reference to the reference" is passed.

What I have tried:

Confused on what is the correct way of explaining pass by Refernece. I WOULD GREATLY APPRECIATE THE HELP. THANKS!
Posted
Updated 28-Apr-21 10:38am
v18

1 solution

When you call a method in C#, the default is to pass by value: a copy is made of the value and that it passed to the method to work with.
Nothing the method does to the variable will affect the outside world:
C#
public void NoAffect(int x)
   {
   Console.WriteLine(x)
   x = 0;
   Console.WriteLine(x)
   }
...
   int myVar = 666;
   NoAffect(myVar);
   Console.WriteLine(myVar);
Will give you:
666
0
666
Because a copy of the myVar variable was made and passed the NoAffect.
This means that you can call NoAffect with a variable or a constant value:
C#
...
   int myVar = 666;
   NoAffect(myVar);
   Console.WriteLine(myVar);
...
   NoAffect(666);
   Console.WriteLine(666);
Will give you the same results.

That is the same regardless of whether the datatype passed is a Value type or a Reference type:
C#
public class MyClass
   {
   public int x;
   }

public void NoAffect(MyClass mc)
   {
   Console.WriteLine(mc.x)
   mc = new MyClass{x = 0};
   Console.WriteLine(mc.x)
   }
...
   MyClass myVar = new MyClass {x = 666};
   NoAffect(myVar);
   Console.WriteLine(myVar.x);
And it will print the same thing:
666
0
666
Because the reference was copied and passed to the method.

OK so far?

When you pass by reference that doesn't happen: a copy isn't passed, but an alias is - the variable in the method is the variable outside it:
C#
public void Affect(ref int x)
   {
   Console.WriteLine(x)
   x = 0;
   Console.WriteLine(x)
   }
...
   int myVar = 666;
   Affect(myVar);
   Console.WriteLine(ref myVar);
Will give you:
666
0
0
Again, the same thing happens with Reference types:
C#
...
		MyClass mc = new MyClass {x = 666};
		Affect (ref mc);
		Console.WriteLine(mc.x);
...
public void Affect(ref MyClass mc)
   {
   Console.WriteLine(mc.x);
   mc = new MyClass{x = 0};
   Console.WriteLine(mc.x);
   }
Because an alias to the reference variable is passed this time.

This means that Affect cannot be called with a constant value at all - you can't pass an alias to a constant, it must be what is called an L-Value: something that can be on the "left hand side" of an assignment operator.

Technically, a reference is passed (even to a value type) but it shouldn't be confused with the "reference" in "reference type" because it's not the same thing - passing a genuine reference to a Value type variable would require boxing and unboxing and that doesn't happen with pass by reference - which is why I called it an alias above. so I'd disagree (and agree!) with both the descriptions in your question!

Does that seem a little clearer?
 
Share this answer
 
Comments
CPallini 27-Apr-21 2:01am    
5.
Wendelius 27-Apr-21 18:55pm    
Nicely explained!
[no name] 27-Apr-21 20:43pm    
OriginalGriff (apologies for the late reply) First off huge thanks for the help once again, I appreciate you taking the time to help me. This post has made my understanding concerning pass by Value much clearer. However, I am still a bit confused with pass by Reference. Don't get me wrong I completely understand the semantics of pass by Value and pass by Reference. In terms of what is affected and what is not affected after the termination of the callee. However, it has been being able to explain it (pass by Reference) with the correct words that has been confusing to me. Hence, me creating this post.

With that being said, you stated "When you pass by reference that doesn't happen: a copy isn't passed, but an alias is - the variable in the method is the variable outside it"... But what does "alias" mean in programming terms???

Is it referring to:
1) a reference to a variable being passed?
2) a reference to a value or a refernce to a reference being passed?

Or maybe it is not referring to any of the above. Then again you did say "so I'd disagree (and agree!) with both the descriptions in your question". I don't mean to sound repetitive. But i'm just trying to clear up what you meant by an "alias" being passed. Thanks.
OriginalGriff 28-Apr-21 4:44am    
From a technical point of view what is passed is a reference, but it's never quite that simple, because you can't take a "normal reference" to a value type as they aren't held as objects on the heap - and a "normal reference" is always to a heap object.
When you need to use a reference to a value type (like adding mixed data to a List of objects for example) the value type is Boxed: it's copied into a heap Object, and a reference to that is used, and Unboxed when it's used - the Object is "opened up" the value type is copied out and used. You cannot take a reference to a value type.

But passing by reference is different - you can pass a "reference" to a value type and what is used inside the method is not boxed at all: it is the external variable, not a copy of it in any way - which is why calling it "a reference" is confusing, and why I prefer to call it "an alias" instead.

So please, stop thinking of references being passed because it's not quite right - it's the same term being used for two slightly different things!
[no name] 28-Apr-21 17:02pm    
Ok. It's starting to be clearer. So then the 2nd description in my post is most incorrect since you say the term “reference” should not be used in such circumstance. And the 1st description of explaining pass by Reference is more aligned with what you are saying?

In your above explanation you in a way described to me what passing an "alias" does not refer to. But I am still confused as to what an alias means in programming terms...

Because in the english language, as you know, an alias means "referring to another name (a secondary name) that something or someone goes by".

Therefore when you or even the Microsoft documentation says "An alias to a variable is passed to the callee when an argument is passed by Reference"... In my mind I am interpreting that as "Another name the variable goes by is passed to the callee when an argument is passed by Reference".

Hence my confusion with the term "alias" in this circumstance. But I assume the term "alias" in this circumstance has a different meaning then its traditional meaning in the english language. I Hope that made any sense in terms of why I am confused.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900