关于这三个关键字之前能够研究一下底本的一些操纵
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(); } } }
视察运转效果发明
值并没有被转变,也就是说此时的操纵的道理能够也是跟之前C言语的函数操纵是一样的
本文重要议论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 无需举行初始化。
以上就是关于C#中三个关键字params,Ref,out的细致引见的细致内容,更多请关注ki4网别的相干文章!