Created
December 30, 2020 15:14
-
-
Save javascripto/c6284d1cc1566814afbf1b8464c719dc to your computer and use it in GitHub Desktop.
Passing arguments as value, reference, and extracting output
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; | |
| namespace Program | |
| { | |
| class Program | |
| { | |
| static void Main(string[] args) | |
| { | |
| ArgumentosPorValorVsPorReferencia(); | |
| } | |
| static void ArgumentosPorValorVsPorReferencia() | |
| { | |
| int x = 2, y = 4; | |
| Console.WriteLine($"x: {x}, y: {y} -> Valor inicial"); | |
| ArgsByValue(x, y); | |
| Console.WriteLine($"x: {x}, y: {y} -> Nada acontece passando argumentos por valor"); | |
| ArgsByReference(ref x, ref y); | |
| Console.WriteLine($"x: {x}, y: {y} -> Referencias de argumentos tem efeitos colaterais"); | |
| ArgsOutput(out int a, out int b); | |
| Console.WriteLine($"a: {a}, b: {b} -> Extraindo valores de output"); | |
| Console.ReadLine(); | |
| } | |
| static void ArgsByValue(int a, int b) | |
| { | |
| int temp = a; | |
| a = b; | |
| b = temp; | |
| } | |
| static void ArgsByReference(ref int a, ref int b) | |
| { | |
| int temp = a; | |
| a = b; | |
| b = temp; | |
| } | |
| static void ArgsOutput(out int a, out int b) | |
| { | |
| a = 1; | |
| b = 2; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment