CSharp - Method out modifier

Introduction

An out argument is used to get value of a method.

You do not need to assign a value to out parameter before passing them into the function.

And you must assign a value to them before it comes out of the function.

You can use out modifier to get multiple return values back from a method.

An out parameter is passed by reference.

For example:

Demo

using System;
class Test// w w  w .j a  va 2  s  . co  m
{
   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 ("this is a test", out a, out b);
         Console.WriteLine (a);                      
         Console.WriteLine (b);                      
    }
}

Result

Related Topics

Quiz