ref 与 out 参数的区别
Ref 与 Out的区别在于,数组类型的 ref 参数必须由调用方明确赋值,使用数组类型的 out 参数前必须先为其赋值
下边是分别用ref 和Out写的两个例子
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication12
{
class Program
{
static void
{
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
{
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 };
}
}
}