Skip to content

Instantly share code, notes, and snippets.

@danShumway
Created September 9, 2014 16:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danShumway/a3d689bf4ca64c65e670 to your computer and use it in GitHub Desktop.
Save danShumway/a3d689bf4ca64c65e670 to your computer and use it in GitHub Desktop.
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