• 引用参数,值参数,ref,out


    1,一个参数只有在引用的时候才能改变其值,这是一种情况

    2,一个参数在引用后要永久的改变其值(可以用返回参数的形式)

    3,多个参数在引用后要永久的改变其值或者多个参数中的部分,多个参数时如果要输出一个参数的话要用OUT(输出多个的话用多个OUT),当然要注意,如果一个参数不是输出值就不能用OUT

    4,OUT就是输出的意思,老外起名不会瞎起的,呵呵

    有时候我们可以“顾名思义”  OK上例子

    1

    static void ShowDouble(int val)
    {
    val *= 2;
    Console.WriteLine("val doubled = {0}", val);
    }
    
    
    int myNumber = 5;
    Console.WriteLine("myNumber = {0}", myNumber);
    ShowDouble(myNumber);
    Console.WriteLine("myNumber = {0}", myNumber);
    View Code

    2

    static int DoubleNum(int val)
    {
    val *= 2;
    return val;
    }
    
    
    
    
    int myNumber = 5;
    Console.WriteLine("myNumber = {0}", myNumber);
    myNumber = DoubleNum(myNumber);
    Console.WriteLine("myNumber = {0}", myNumber);
    View Code

    3。REF

    static void ShowDouble(ref int val)
    {
    val *= 2;
    Console.WriteLine("val doubled = {0}", val);
    }
    
    
    int myNumber = 5;
    Console.WriteLine("myNumber = {0}", myNumber);
    ShowDouble(ref myNumber);
    Console.WriteLine("myNumber = {0}", myNumber);
    View Code


    4. OUT

    static int MaxValue(int[] intArray, out int maxIndex)
    {
    int maxVal = intArray[0];
    maxIndex = 0;
    for (int i = 1; i < intArray.Length; i++)
    {
    if (intArray[i] > maxVal)
    {
    maxVal = intArray[i];
    maxIndex = i;
    }
    }
    return maxVal;
    }
    
    
    
    int[] myArray = { 1, 8, 3, 6, 2, 5, 9, 3, 0, 2 };
    int maxIndex;
    Console.WriteLine("The maximum value in myArray is {0}",
    MaxValue(myArray, out maxIndex));
    Console.WriteLine("The first occurrence of this value is at element {0}",
    maxIndex + 1);
    View Code
  • 相关阅读:
    Boliuraque OI 总结
    HNU 1447 最长上升路径
    妹纸
    某个子串的循环节
    跳石头
    小澳的葫芦
    递推式的循环问题
    BZOJ 2326 数学作业
    BZOJ 2337 XOR和路径
    hdu5468 Puzzled Elena
  • 原文地址:https://www.cnblogs.com/mamiyiya777/p/5939472.html
Copyright © 2020-2023  润新知