zoukankan      html  css  js  c++  java
  • 参数与返回值

    经常使用函数,下面对函数的重要内容--参数,进行一个总结

    在C#中函数调用传递的参数可以分为4类:值参数、引用参数、输出参数、数组参数。下面一一对他们讲解

    1.值参数(value parameter)

    他就是我们经常说的型参,实质就是对实参的拷贝,并没有对实参进行操作

      class Program
        {
           
            static void Main(string[] args)
            {
                Class1 class1 = new Class1();
                class1.show("传递一个消息");
            }
        }
        class Class1 
        {
            public void show(string message)
            {
                Console.WriteLine(message);
                Console.ReadLine();
            }
        }
      
    

      2.引用参数(reference parameter)

    主要传递的是参数的引用指针,用以辅助执行传递地址的操作。意思就是传值得时候传的是地址,可以对实参进行操作,和形参的拷贝值不同。

    使用ref类型参数的函数可以实现对外部传递过来的参数进行加工。

    static public void Arr(ref int[] arr) 
            {
                arr[0] = 100;
                arr[2] = 6634;
                arr[4] = 0;
            }
            static void Main(string[] args)
            {
                int[] arr = new int[8] { 1, 2, 3, 4, 5, 6, 7, 8 };
                Arr(ref arr);
                for (int i = 0; i < arr.Length; i++)
                {
                    Console.WriteLine(arr[i]);
                    Console.ReadLine();
                    //结果是100,2,6634,4,0,6,7,8,可见引用参数把实参进进行了操作。
                }
            }
        
    

      3.输出参数(output parameter)

    输出参数也是引用传递,这种参数类型在函数超过一个以上的返回值时使用。使用out关键字一般是为了让一个方法有多个返回值。

     static public void Arr(out int[] arr) 
            {
                //初始化数组
                arr = new int[8] { 111, 112, 113, 114, 115, 116, 117, 118 };
    
            }
            static void Main(string[] args)
            {
                int[] arr;
                Arr(out arr);
                for (int i = 0; i < arr.Length; i++)
                {
                    Console.WriteLine(arr[i]);
                    Console.ReadKey();
                }
                //结果  111,112,113,114,115,116,117,118
            }
    

      通过上面的比较可以得到,ref和out传递的都是参数的地址。而他们的区别在于,数组类型的ref参数必须由调用方明确赋值,而使用数组类型的out参数前必须由定义函数先为其赋值。

         ref有些类似C语言中的指针。传递参数前一定要赋值。ref不允许传递 ref string

        out参数调用不必初始化,即不用赋值。而在被调用的函数内部必须至少赋值一次。但属性不能作为out参数传递。

    4.数组参数(array parameter)

    params 类型参数类型主要用于不知道数组长度的情况下进行函数的声明。

    static public void Add(params int[] args) 
            {
                int Count = 0;
                foreach (int  a in args)
                {
                    Count += a;
                }
                Console.WriteLine("0",Count);
                Console.ReadKey();
               
            }
            static void Main(string[] args)
            {
                int[] arr = new int[8] { 1, 2, 3, 4, 5, 6, 7, 8 };
                Add(arr);
                
            }
    

      这种类型的参数在数据的连接方面有很好的效果。

  • 相关阅读:
    Linux基础命令(二)
    Linux基础命令(一)
    安装Centos 7操作系统
    网络基础之网络协议
    操作系统简介
    计算机硬件知识
    VS快捷键
    VB.NET 编程元素支持更改总结
    VB.Net中确认退出对话框的实现
    VB定义变量
  • 原文地址:https://www.cnblogs.com/wohaoxue/p/4197153.html
Copyright © 2011-2022 走看看