• 0

Ref in C#


Question

Is there a way to assign the "ref" of a variable to another variable in C#?

I mean, passing the "ref" of a variable through a method is ok, but can we then assign that "ref" to another variable (a global var) ?

Link to comment
Share on other sites

8 answers to this question

Recommended Posts

  • 0

Actually, Im using Visual C# 2005.

And global variable or not, what I want is to put the ref (not value) of a var into another var.

E.g:

var1 = (ref) var2

Edited by GoD oF SouL
Link to comment
Share on other sites

  • 0

ok, well this should do the trick..

protected void Button1_Click(object sender, EventArgs e)
{
	string x = "something";

	doStuff(ref x);
	Label1.Text = x; // this will be "somethingelse"
}

void doStuff(ref string s)
{
	s = "somethingelse";
}

Link to comment
Share on other sites

  • 0

why not use a property? :

private string one;
private string two;

public string One	  //notice Upper case
{
	 get
	 {
		   return one;
	 }
	 set
	 {
		   one = value;
		   two = value;
	 }
}


public string Two
{
	get
	{
		 return two;
	}
}

so instead of using "one" directly, use "One" which will automatically write the same value into the strings "one" and "two"

Also if you wish to read the value of "two", use instead the "Two" property which is read only

Link to comment
Share on other sites

  • 0

The problem you are seeing is one of the differences between value types and reference types. Ignoring the 'ref' concept for now, you should look at what makes a value type, and what makes a reference type.

Reference types don't actually store the intended value, but they store the memory address of that intended value. That way you could do something like this:

MyClass file1 = new MyClass() { Text = "Hello"; }
MyClass file2 = file1;

file1.Text = "World";
Console.WriteLine(file2.Text);

And expect the output of Console.WriteLine(...) to be "World" because file1 and file2 point to the same memory location.

Value types don't do this:

int number1 = 2;
int number2 = number1;

number1 = 74;
Console.WriteLine(number2);

That should output the value 2, not the value 74, as the variable number2 does not hold a memory address, but its own value.

As GreenMartian has said, pointers would be the way to do this, so I could do the following:

int *number1 = 2;
int *number2 = &number1;

*number1 = 74;
Console.WriteLine(number2);

And I would expect the output of variable number2 to be 74, as it is a pointer to the other variable number1 (well, a pointer to its memory address).

This is generally regarded as bad practice, as it is invoking unmanaged/unsafe code. C# (well .NET) employs an efficient memory manager and garbage collector, which will not manage [the disposal of] the pointers and free up the used memory for you. There is also a couple of other side effects:

  1. You can only use pointers in code blocks marked as unsafe.
  2. Unsafe code can only be run in Full-trust.

Don't get the concept of reference/value types mixed up the the ref/out modifiers in method parameters. They are very different things.

Link to comment
Share on other sites

  • 0

I was pretty mixed up with reference types when I started C#, so take your time to understand them. Antaris' explanation is perfect.

What happens when you are passing a reference type to a function, is that you are making a copy, not of the object, but of the reference to that object. There are now two references to the same object: one inside the function call, and one outside. Both can be used to read and modify the same object, so any modifications to the object in the function will be seen by the caller when the function returns.

However, if you reassign the copy to another object, the original reference will still point to the original object. In C#, object references are passed by value. If you can understand that, you've understood everything. This is where ref comes in. Ref allows you to pass object references by reference, so any changes made to the reference modify the original too.

Let's illustrate this with an example:

using System;

namespace ConsoleApplication1 {
	class MyClass {
		public string Text { get; set; }
	}

	class Program {
		static void Main(string[] args) {
			var obj = new MyClass() { Text = "Hello" };
			Console.WriteLine(obj.Text);

			modifyObject(obj);
			Console.WriteLine(obj.Text);

			modifyReference(ref obj);
			Console.WriteLine(obj.Text);
		}

		static void modifyObject(MyClass b) {
			b.Text = "World";
		}

		static void modifyReference(ref MyClass c) {
			c = new MyClass() { Text = "Rabbit" };
		}
	}
}

This outputs, as intended:

Hello

World

Rabbit

If we remove the ref keyword, this outputs:

Hello

World

World

Can you see why now?

Edited by Dr_Asik
Link to comment
Share on other sites

  • 0

Thanks for all your answers. I studied them and came to the conclusion that:

Although I also thought of pointers, it would be an unnecessary complexity since a simpler approach could be taken. Thx.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.