zoukankan      html  css  js  c++  java
  • .Net之方法重载(以及out与ref的区别)

       class Program
        {
            static void Main(string[] args)
            {
                //int[] arr = new int[3];
                //TestParams(arr);
                //int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
                ////  TestParams(arr); 
                //int[] a = new int[0];
                //TestParams(1, 2, 3, 4, 5); 
    
                //int i = 12;
                //TestOut(out i);
                //Console.WriteLine(i);
    
                //int[] arr = { 1, 2, 5, 4, 3 };
                //int min = 0;
                //int max = 0;
                //GetMaxAndMin(arr, out max, out min);
                //Console.WriteLine(min+":"+max);
    
                int num=0; 
                TestRef(ref num);
                Console.WriteLine(num);
                //TestOut(out num); 
                //Console.WriteLine(num);
    
              
    
                Console.ReadKey();
            }
    
    
            //在同1个作用域下 不能定义相同名字的成员.
            //什么情况下可以构成方法重载:
            //1. 方法的名字一样 2.方法参数的个数或者类型或者顺序不一样 3.必须在同1个类中. 4.与返回值无关.
            //可变参数 参数被params修饰 params只能用来修饰1维数组
            //给可变参数赋值的时候 可以直接传递数组的元素.
            //在调用的时候 会自动的将这些元素封装为1个数组 并将数组传递.
            //可变参数必须放在参数列表的最后. 
            //ref 修饰方法的参数  在调用的时候必须在变量前面加上ref关键字. 只能传递变量不能传递常量.
            //传递的时候 不是传递变量的值 而是传递变量的地址.
            //out 也是传递的变量的地址.out必须在方法内为其赋值.ref可以修改其值也可以不修改.
            //out侧重于输出 ref侧重于修改. 
            //out在传递之前可以不赋初始值 因为在方法中肯定会为out赋值.
            //ref 在传递之前必须要有值 因为在方法中有可能会用到这个参数的值.
    
    
            static void TestOut(out int i)
            {
                string str = "s";
                if (str == "s")
                {
                    i = 1;
                }
                else
                {
                    i = 2;
                } 
            } 
    
            static void TestRef(ref int i)
            {
                i = i + 1;
               
            }
    
             //冒泡排序
            static void GetMaxAndMin(int[] arr, out int max, out int min)
            { 
                for (int i = 0; i < arr.Length - 1; i++)
                {
                    for (int j = 0; j < arr.Length - 1 - i; j++)
                    {
                        if (arr[j] > arr[j + 1])
                        {
                            int temp = arr[j];
                            arr[j] = arr[j + 1];
                            arr[j + 1] = temp;
                        }
                    }
                }
                max = arr[arr.Length - 1];
                min = arr[0];
            }
    
    
    
            //static void TestParams(int i, int j, params int[] arr)
            //{
    
            //} 
    
            static void Test(int i, string str)
            {
    
            }
    
            static void Test()
            {
                Console.WriteLine("B");
            }
        }
  • 相关阅读:
    windows的一组常用运行命令
    nfs:server is not responding,still trying 原因与解决方案
    MYSQL外键(Foreign Key)的使用
    byte[]转字符串编码问题
    /usr/bin/ld: cannot find lGL
    Linux查看用户及分组
    NAND和NOR flash的区别
    Win7+Ubuntu11.10(EasyBCD硬盘安装)
    Win7+Ubuntu12.04.1硬盘安装错误及解决方案
    Linux内核编译时错误
  • 原文地址:https://www.cnblogs.com/kongsq/p/5866681.html
Copyright © 2011-2022 走看看