Created
February 15, 2012 14:43
-
-
Save ChrisMoney/474d06200bedae73c30d to your computer and use it in GitHub Desktop.
C# --Pass Value by Argument
This file contains 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
// Pass value type arguments | |
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(I, 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 = 2 | |
After the call to Update(int. double): | |
I = 1, d = 2 | |
Press enter to terminate… |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment