marcus-s
5/8/2019 - 3:22 PM

Passing a var by reference in C#

Passing a var by reference in C#

using System;
					
public class Program
{
	public static void Main()
	{
		bool foo = false;
		Console.WriteLine("foo 1: ");
		Console.WriteLine(foo);
		changeFoo(ref foo);
		Console.WriteLine("foo 2: ", foo);
		Console.WriteLine(foo);
	}
	private static void changeFoo(ref bool fooRef) 
	{
		fooRef = true;
	}
}

// output
// foo 1:
// false
// foo 2: 
// true