Skip to content

Instantly share code, notes, and snippets.

@ChrisMoney
Created February 15, 2012 14:41
Show Gist options
  • Save ChrisMoney/5898521eb2f78ce94e6f to your computer and use it in GitHub Desktop.
Save ChrisMoney/5898521eb2f78ce94e6f to your computer and use it in GitHub Desktop.
C# --Pass Value by Reference
// Pass By Reference
using System;
namespace PassByValue
{
public class Program
{
// Update – try to modify the values of the arguments passed to it; note that you can declare
// functions in any order in a class
public static void Update(int I, double d)
{
i = 10;
d = 20.0;
}
public static void Main(string[] args)
{
// declare two variables and initialize them
int I = 1;
double d = 2.0;
Console.Write(“Before the call to Update(int, double):”);
Console.Write(“I = “ + I + “, d =” + d);
// invoke function
Update(ref i, out d);
// notice that the vales 1 and 2.0 have not changed
Console.WriteLine(“After the call to Update(int, double);”);
Console.WriteLine(“i = “ + I + “, d = “ + d);
// wait for user to acknowledge
Console.WriteLine(“Press Enter to terminate…”);
Console.Read();
}}}
Output:
Before the call to Update(int, double):
I = 1, d is not intialized
After the call to Update(int. double):
I = 10, d = 20
Press enter to terminate…
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment