ref 与 out 参数的区别
Ref 與 Out的區別在于,數組類型的 ref 參數必須由調用方明確賦值,使用數組類型的 out 參數前必須先為其賦值
?
下邊是分別用ref 和Out寫的兩個例子
?
using System;
using System.Collections.Generic;
using System.Text;
?
namespace ConsoleApplication12
{
???? class Program
???? {
???????? static void Main(string[] args)
???????? {
????????????? int[] myArray =new int [5]{1,2,3,4,5};
????????????? ?FillArray(ref? myArray);
??????????? for (int i=0; i < myArray.Length; i++)??????
???????? ??? Console.WriteLine(myArray[i]);
???????? }
????????
???????? static public void FillArray(ref? int[] myArray)
???????? {
????????????? // Initialize the array:
????????????? myArray[0] = 123;
???????? }
?
???? }
?
}
?
?
?
using System;
using System.Collections.Generic;
using System.Text;
?
namespace ConsoleApplication12
{
???? class Program
???? {
???????? static void Main(string[] args)
???????? {
????????????? int[] myArray; // Initialization is not required
??????????? FillArray(out myArray);
?????????? for (int i=0; i < myArray.Length; i++)?
???? ??????? Console.WriteLine(myArray[i]);
???????? }
????????
???? ???? static public void FillArray(out int[] myArray)
???????? {
????????????? // Initialize the array:
????????????? myArray = new int[5] { 1, 2, 3, 4, 5 };
???????? }
?
???? }
?
?
}
轉載于:https://www.cnblogs.com/AlonaLiu/archive/2008/12/15/1355518.html
總結
以上是生活随笔為你收集整理的ref 与 out 参数的区别的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 快手文档 - www.kuaishou.
- 下一篇: 常用的SQL收集