Created
September 9, 2014 16:31
-
-
Save danShumway/a3d689bf4ca64c65e670 to your computer and use it in GitHub Desktop.
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 characters
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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment