using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ref_out { class Program { static void Method1(ref int ...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ref_out
{
class Program
{
static void Method1(ref int i)
{
i = 44;
}
static void Method2(out int j)
{
j = 55;//out 內部必須要有值
}
static void Method3(ref int h)
{
//ref 內部可以為空,為空時得到的值是初始化值;不為空時得到的是ref傳出的值
}
static void Method4(ref int h)
{
h += 11;
}
static void Main()
{
int val1 = 0;
int val2;
int val3 = 66;
int val4 = 66;
Method1(ref val1);//使用ref時 要先初始化。傳入傳出
Method2(out val2);//可以不初始化,傳出值
Method3(ref val3);
Method4(ref val4);
Console.WriteLine(val1);//44
Console.WriteLine(val2);//55
Console.WriteLine(val3);//66
Console.WriteLine(val4);//77
Console.ReadKey();
}
}
}