Total Pageviews

Tuesday, 10 January 2012

Out keyword in C#

What is out.. ? Out keyword is like ref but here you must not initialize the variable before passing to calling method. ,
Purpose of this keyword when you want to returned one or more than one value from method.
Simply we explicitly copies the reference or address from calling method store at stack and again rewritting it to that references or addresses and return back to calling method from method called.

Here are some snaps, that will explore the work flow of Out keyword.

Look at Snap No.1 :

  Typically program will start to execute from Main method, here i have taken three variables and i will pass  these variable to calling method with out keyword, means i am passing addresses through argument to called method parameter.

Look at Snap No.2 :

Here method is going to call with out keyword ...


 Look at Snap No.3 :

 Here we explicitly copying reference or addresses from calling method to method parameter  para_One,para_Two and para_Three.

Picturized memory locations for para as :

     para_One                 para_Two                  para_Three

 null
null
null
20001
20002
20003



 Look at Snap No.4 :

 I have assigned text to para_One,para_Two and para_Three.


 Look at Snap No.5 :

 Greetings !!! all values from method got returned and rewritten to address [20001], [20002], [20003] which are previously passed through calling method. 

 What we did ? Simply copied that text to address loaction.

 arg_One                       arg_Two                  arg_Three
argument
'arg_one'add = 20001 ..
         value part
argument 'arg_one'add = 20002..
         value part
argument 'arg_one'add = 20003 ..
             value part
20001    add part
20002    add part
20003    add part



   Look at Snap No.6 :

  OutPut:


Full Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

            namespace ConsoleApplication1
             {
              class out_keyword
               {
                public static void method(out string para_One,out string para_Two,out string para_Three)
                 {
para_One = "argument 'arg_One' address = 20001 from calling method and" + Environment.NewLine + "copied into called method parameter 'para_One' and rewritten to arg_One";
para_Two = "argument 'arg_Two' address = 20002 from calling method and" + Environment.NewLine + "copied into called method parameter 'para_Two' and rewritten to arg_Two";
para_Three = "argument 'arg_Three' address = 20003 from calling method" + Environment.NewLine + "copied into called method parametere 'para_Three' and rewritten to arg_Three";   
                 }
        public static void Main(string[] args)
        {
            string arg_One, arg_Two, arg_Three;

            method(out arg_One, out arg_Two, out arg_Three);
            Console.WriteLine("{0}\n\n{1}\n\n{2}",arg_One,arg_Two,arg_Three);
            Console.ReadKey();
        }

    }
}

No comments:

Post a Comment