Tuesday, 7 February 2012

How to use out,ref,params,args parameter modifiers?


method variable are 3 types

value type(default)
out variable
ref variable.
Yes,By default function parameters are value type parameters. When call a method
A copy of data is passed to called method not original data.
Ex:
class valueparameters
    {
        static void Main()
        {
            int x = 10;
            int y = 20;  // original data         
            Console.WriteLine(Sample(x, y));
            Console.Read();
        }
        public static int Sample(int x, int y) // value parameters default(a copy of data is passed here
        {
            int z = x + y;
            x = 100;
            y = 200;
            return z; // returns 30 not 300
       
        }
    }


Out parameter : out parameter is used to return a method multiple return values or additional return values from a method.

Note: Our parameter no need to initialize before calling method

        static void Main()
        {
            int z; // no need to initialize before passing to called method
            Add(10, 20, out z); // caller
            Console.WriteLine(z);
            Console.Read();
       
        }
        public static void Add(int x,int y,out int z)  //called
        {

            z = x + y;

        }
These return values are stored in,the same variables like in our example z;
Ref Parameter : ref parameter is used to return a method multiple return values/ addition return values form a method 
but in case of ref parameter we should initialize the ref variables, before caller method calling the called method.

        static void Main()
        {
            int i; // variable need to be initialized
            i = 3;
            RefMethod(ref i);
            Console.WriteLine(i);
            Console.Read();
       
        }
        private static void RefMethod(ref int i)
        {
            i += 5;
        }
 Params Parameter : Params parameter is used to access the same type of values from a single variable.
   static void Main()
        {
            int[] intarr = new int[] { 10, 20, 30, 40 };
            TestMethod(intarr);         
            Console.Read();               
        }
        public static void TestMethod(params int[] values)
        {
            IEnumerator ier = values.GetEnumerator();
            while (ier.MoveNext())
            {
                int i = (int)ier.Current;
           
                Console.WriteLine(i);
             
            }
                  
        }
Args Parameter: args parameter is used to access any type of parameters array from Commandline window of the project
Ex:
class argsParameter
    {

        static void Main(string[] args)
        {
            //Console.WriteLine(args.Length);
            IEnumerator ier = args.GetEnumerator();
            while (ier.MoveNext())
            {
                string s =(string) ier.Current;
                Console.WriteLine(s);
               
            }
            Console.Read();
        }
     
       
    }
So Main accepts arguments from the commandpromt and run the application
C:\Documents and Settings\vijaya_g\My Documents\Visual Studio 2008\Projects\InterfaceDemo\InterfaceDemo\bin\Debug
Goto path
C:> cd C:\Docuements and settings…..>InterfaceDemo.exe kasi babu nani
out put: Kasi babu nani
C:> cd C:\Docuements and settings…..>InterfaceDemo.exe 10,20
out put : 10 20
So main method also returns the values
We can specify the Main signatre 4 ways like
 Main(),Main(stings[] args),Main(int[] args)

out: out means we excepting the out put from the method. like out put variable in stored procedure.
why these out variables---> are used to return a method more then one variables also from single method inviation.

 so not be required to initialize calling the out put variable method. and called method should fill this out variable. why because we excepting the variable.


 For more References:


http://www.yoda.arachsys.com/csharp/parameters.html#gloss




No comments:

Post a Comment