zoukankan      html  css  js  c++  java
  • 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. 
  • 相关阅读:
    Codeforces1420E. Battle Lemmings 题解 动态规划
    C++使用partial_sum求前缀和
    HDU6171 Admiral 题解 折半搜索
    HDU3017 Treasure Division 题解 折半搜索
    使用re.split 按标点+空格的一种分割办法
    实现CString的Format功能,支持跨平台
    转载一篇makefile,说的很详细
    Microsoft C++ 异常: std::system_error std::thread
    源码一样,运行结果不一致的解决办法
    记录一次阿里的电话面试
  • 原文地址:https://www.cnblogs.com/tianjinquan/p/2113302.html
Copyright © 2011-2022 走看看