Skip to content

Instantly share code, notes, and snippets.

@javascripto
Created December 30, 2020 15:14
Show Gist options
  • Select an option

  • Save javascripto/c6284d1cc1566814afbf1b8464c719dc to your computer and use it in GitHub Desktop.

Select an option

Save javascripto/c6284d1cc1566814afbf1b8464c719dc to your computer and use it in GitHub Desktop.
Passing arguments as value, reference, and extracting output
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