Created
September 9, 2014 16:31
Revisions
-
danShumway created this gist
Sep 9, 2014 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,43 @@ using System.IO; using System; class Program { static void Main() { int x = 5; int y = 10; //I promise, C# treats arrays like objects. int[] xObject = new int[1]; xObject[0] = 5; int[] yObject = new int[1]; yObject[0] = 10; playingWithRefs(ref x, ref y); Console.WriteLine(x); //10 alsoAReferenceButReferenceMeansADifferentThingHere(xObject, yObject); Console.WriteLine(xObject[0]); //5 passingObjectsByReferenceUsingRef(ref xObject, ref yObject); Console.WriteLine(xObject[0]); //10 } //We specify to pass by reference wit the ref keyword. static void playingWithRefs(ref int a, ref int b) { a = b; } //We're passing by reference in the sense that we're passing in objects. static void alsoAReferenceButReferenceMeansADifferentThingHere(int[] a, int[] b) { a = b; } static void passingObjectsByReferenceUsingRef(ref int[] a, ref int[] b) { a = b; } }