• C# 中三个关键字params,Ref,out


    一、

    using System;

    using System.Collection.Generic;

    using System.Text;

    namespace ParamsRefOut

    {

    class Program

    {

    static void ChangeValue(int i)

    {

    i=5;

    Console.WriteLine("The ChangeValue method changed the value "+i.ToString());

    }

    static void Main(string[] args)

    {

    int i=10;

    Console.WriteLine("The value of I is"+i.ToString());

    ChangeValue(i);

    Console.WriteLine("The value of I is"+i.ToString());

    Console.ReadLine();

    }

    }

    }

    本文主要讨论params关键字,ref关键字,out关键字

    1)params关键字,官方给出的解释为用于方法参数长度不定的情况。有时候不能确定一个方法的方法参数到底有多少个,可以使用params关键字来解决问题

    using System;

    using System.Collections.Generic;

    using System.Text;

    namespace ParamsRefOut

    {

    class number

    {

    public static void UseParams(params int[] list)

    {

    for(int i=0;i<list.Length;i++)

    {

    Console.WriteLine(list[i]);

    }

    }

    static void Main(string[] args)

    {

    UseParams(1,2,3);

    int[] myArray=new int[3]{10,11,12};

    UseParams(myArray);

    Console.ReadLine();

    }

    }

    }

    2)ref关键字:使用引用类型参数,在方法中对参数所做的任何更改都将反应在该变量中

    using System;

    using System.Collections.Generic;

    using System.Text;

    namespace ParamsRefOut

    {

    class number

    {

    static void Main()

    {

    int val=0;

    Method(ref val);

    Console.WriteLine(val.ToString());

    }

    static void Method(ref int i)

    {

    i=44;

    }

    }

    }

    (3)out关键字:out与ref相似但是out无需进行初始化

  • 相关阅读:
    Python openpyxl的使用
    Python字典的遍历
    Python3元组的简介和遍历
    tableau绘制饼图
    tableau创建点位地图
    tableau绘制热力地图
    select下拉框分组使用bootstrap select
    css设置div中table水平居中
    sql server的分组排序partition by函数
    document.ready
  • 原文地址:https://www.cnblogs.com/zzp0320/p/7059750.html
Copyright © 2020-2023  润新知