The out modifier - CSharp Custom Type

CSharp examples for Custom Type:Method Parameter

Introduction

An out argument is like a ref argument, except it:

  • Need not be assigned before going into the function
  • Must be assigned before it comes out of the function

The out modifier is used to get multiple return values back from a method.

Like a ref parameter, an out parameter is passed by reference.

Demo Code

using System;/*from   ww w.j  av  a  2  s . co m*/
class Test
{
   static void Split (string name, out string firstNames, out string lastName)
   {
      int i = name.LastIndexOf (' ');
      firstNames = name.Substring (0, i);
      lastName   = name.Substring (i + 1);
   }
   static void Main()
   {
      string a, b;
      Split ("Stevie Ray Vaughan", out a, out b);
      Console.WriteLine (a);                      // Stevie Ray
      Console.WriteLine (b);                      // Vaughan
   }
}

Result


Related Tutorials