I'm really new to C# so this may seem like a novice question..How does C# accomplished passing a reference to a object reference? I'm really trying to understand the underlying mechanisms and how it applies to the attached code with the comments - //what happens here? Do we pass a ref->ref->ref....or is this accomplished via reference counting.

using System;

class my_int{
	int itsx;
	int itsy;
	
	public my_int(int x, int y){
		itsx = x;
		itsy = y;
	}
	
	public void display_it(){
		Console.WriteLine("x->{0}, y->{1}", itsx, itsy);
	}
}

namespace testit
{
	class MainClass{
		
		static void myfunc(ref my_int m, int x){
			if ( x < 10 )
				myfunc(ref m, ++x);//what happens here? Do we pass a ref->ref->ref....or is this accomplished via reference counting.
			else
				m = new my_int(888, 999);
		}
		
		static void Main(string [] args){
			my_int me = new my_int(123, 456);//create a reference 'me' to a my_int object
			
			me.display_it();
			
			myfunc(ref me, 0);//pass a reference to the reference 'me'.
			
			me.display_it();
		}
	}
}

Recommended Answers

All 4 Replies

Classes in C# are passed by reference by default; adding ref means you are passing a reference to the variable.
So myfunc(ref me, 0) is passing a reference (or pointer) to me.

This allows myfunc to change the value held in me (which is what you do in your else clause), not just the contents of the m_int instance pointed to by me.

Since m in myfunc has been effectively de-referenced I think that the myfunc(ref m, ++x) call is re-referencing the original me.
Therefore I think this will result in reference counting.

BTW: For this snippet all the refs could be removed if you were to add a set method to allow changing the field values of my_int.

Hi there, this article might be of interest to you. Essentially, even without the "ref" keyword you're passing by reference. The ref keyword allows you to make changes to that data. So by adding the keyword, you're allowing the receiving method to change the data. Both cases (with and without ref) pass by reference (which is the reference to the original object).
Without the "ref" keyword, it would act like a value type (even though it's passed as a reference type)

Thank-you for the replies. I understand passing by value(both values and references), my problem lies in passing by reference. I really don't have an intuitive understanding of the mechanism, especially when we use 'pass by ref' in a deeply nested function call.

I can understand a simple pass by reference, passing a reference(by possibly using a pointer). I can't understand how a more general case would work...A case like the above code. Does the compiler keep generating deeper references or does it generate a reference for the function call and keep incrementing that one reference for each nested call...I hope this makes sense.

EDIT: Posted to wrong thread, sorry ^^

In answer to your question, as far as I can tell, it simply passes that reference.

0xAAA565F <-- reference. As far as I'm aware, if you're passing by reference, that's all that gets passed. If you pass it normally, I believe. If you try and over-write that reference, it simply generates a new one, unless you use the ref keyword, in which case it over-writes the old one. Not 100% sure, please correct me if I'm wrong Nick

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.