欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解C#中三個(gè)關(guān)鍵字params,Ref,out

 更新時(shí)間:2017年05月19日 11:04:06   作者:RynerLute  
本文主要討論params關(guān)鍵字,ref關(guān)鍵字,out關(guān)鍵字。非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧

關(guān)于這三個(gè)關(guān)鍵字之前可以研究一下原本的一些操作

using System;
using System.Collections.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();
    }
  }
}

觀察運(yùn)行結(jié)果發(fā)現(xiàn)

值并沒有被改變,也就是說此時(shí)的操作的原理可能也是跟以前C語(yǔ)言的函數(shù)操作是一樣的

本文主要討論params關(guān)鍵字,ref關(guān)鍵字,out關(guān)鍵字。

  1)params關(guān)鍵字,官方給出的解釋為用于方法參數(shù)長(zhǎng)度不定的情況。有時(shí)候不能確定一個(gè)方法的方法參數(shù)到底有多少個(gè),可以使用params關(guān)鍵字來解決問題。

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關(guān)鍵字:使用引用類型參數(shù),在方法中對(duì)參數(shù)所做的任何更改都將反應(yīng)在該變量中

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 關(guān)鍵字:out 與ref相似但是out 無需進(jìn)行初始化。

以上所述是小編給大家介紹的C#中三個(gè)關(guān)鍵字params,Ref,out,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評(píng)論