zoukankan      html  css  js  c++  java
  • c#函数的参数类型

    参数类型主要包括两种

    一、值类型

    在使用值类型参数时,是把一个值传递给函数使用的一个变量。对函数中此变量的任何修改都不影响函数调用中指定的参数

    二、引用类型

    可以影响参数,引用类型的作用还是比较大。会在以后的实战项目中经常碰到,暂不多说,下面只给一个关于引用类型的例子

    其中ref和out的区别在于out在主函数中不需赋值,ref一定要赋值

     
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ConsoleApplication1
    { 
        class Program
        {
            static int IntTest(int i)
            {
                i *= 2;
                return i;
            }
            static void RefTest(ref int i)
            {
                i = 1;
                i = i * 2;
                Console.WriteLine("{0}", i);
            
            }
            static int MaxArray(int[] array ,out int index)
            {
                int max=array[0];
                 index=0;
                 for (int i = 0; i<array.Length; i++)
                 {
                     if (max < array[i])
                         max = array[i];
                     index = i;
                 }
    
                return max;
            }
            static void Main(string[] args)
            {
                #region//ref测试
                int m= 1;//不用加ref在此必须给m赋值
                Console.WriteLine("{0}", m);
                RefTest(ref m);//在此注意要添加ref 
                Console.WriteLine("{0}", m);
                #endregion
    
    
                #region//int测试
                int n = 1;
                Console.WriteLine("{0}", n);
                Console.WriteLine("{0}", IntTest(n));
                Console.WriteLine("{0}", n);
                #endregion
    
    
                #region//out测试
                int[] arry = { 1,2,3,4,5,6,7,7,7,7};
                int index;//在此无须给index赋值,注意与ref的比较
                Console.WriteLine("{0}", MaxArray(arry, out index));
                Console.WriteLine("{0}", index);//
                #endregion
    
                Console.ReadKey();
    
            }
        }
    }
    

    最终的值为12212179(为节省空间,就这样写了)


  • 相关阅读:
    LeetCode153.寻找旋转排序数组中的最小值
    LeetCode88.合并两个有序数组
    分析树
    LeetCode119.杨辉三角 II
    ssh传输文件
    ubuntu arm妙算加载cp210x驱动
    terminator终端工具
    ros使用rplidar hector_mapping建地图
    launch文件
    eclipse配置ros cakin编译环境
  • 原文地址:https://www.cnblogs.com/lzhp/p/2680803.html
Copyright © 2011-2022 走看看