Params, In, Out

 You can get the difference between the" Params and  in or  Out  from debug the code. 
 
using System;

namespace TestIP
{
    class gump
    {
        #region using ref

        public double square(ref double x)
        {
            x = x * x;
            return x;
        }

        public void math_routines(double x, out double half, out double square, out double cube)
        {
            half = x / 2;
            square = x * x;
            cube = x * x * x;
        }

        #endregion

        #region using params

        public  void UseParams(params int[] list)
        {
            string temp = "";
            for (int i = 0; i < list.Length; i++)
            {
                temp = temp + " " + list[i].ToString();
                Console.WriteLine(temp);
            }
        }

        public void UseParams2(params object[] list) 
        { 
            string temp = ""; 
            for (int i = 0; i < list.Length; i++) 
            {
                temp = temp + " " + list[i].ToString(); 
            }
            Console.WriteLine(temp);
        }

        #endregion
    }

    class refoutdemo
    {
        public static void Main()
        {

            gump doit = new gump();

            #region test params

            doit.UseParams(1, 2, 3);//看参数是3个 
            doit.UseParams(1, 2); //看参数是2个,可变吧 
            doit.UseParams2(1, 'a', "test"); 
            int[] myarray = new int[3] { 10, 11, 12 }; 
            doit.UseParams(myarray); //看也可以是容器类,可变吧:) 


            #endregion

            #region test ref

            double x1=600;
            double half1=0;
            double squared1=0;
            double cubed1=0;

            Console.WriteLine("Before method->x1={0}",x1);
            Console.WriteLine("half1={0}",half1); 
            Console.WriteLine("squared1={0}",squared1);
            Console.WriteLine("cubed1={0}",cubed1);

            doit.math_routines(x1,out half1,out squared1,out cubed1);
            
            Console.WriteLine("After method->x1={0}",x1);
            Console.WriteLine("half1={0}",half1);
            Console.WriteLine("squared1={0}",squared1);
            Console.WriteLine("cubed1={0}",cubed1);

            #endregion

            #region test out

            double a = 3;
            double b = 0;

            Console.WriteLine("Before squere -> a={0}, b ={1}", a, b);

            b = doit.square(ref a);
            Console.WriteLine("Before squere -> a={0}, b ={1}", a, b);
            Console.WriteLine();

            #endregion
        }
    }
}
Reference from others. 

 

posted @ 2011-07-21 22:08  Space Tian  阅读(312)  评论(0编辑  收藏  举报