Friday, June 11, 2010

C# - Pasar Variable por Referencia

Aqui un ejemplo básico de cómo se hace:


Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1.  class Program
  2.     {
  3.         static void Main(string[] args)
  4.         {
  5.             string mylocalvar = "my original value";
  6.             Console.WriteLine(mylocalvar);
  7.             changeValue(ref mylocalvar);
  8.             Console.WriteLine(mylocalvar);
  9.         }
  10.  
  11.         static void changeValue(ref string myvar)
  12.         {
  13.             myvar = "new value";
  14.         }
  15.     }


Y esta es la salida al compilar y ejecutar:

Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. my original value
  2. new value
  3. Presione una tecla para continuar . . .

No comments: