Ref 一定要事前給值
Out 一定要 function 離開前給值
程式碼:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int number = 100;
Console.WriteLine("number = {0}",number);
TestRef(ref number);
Console.WriteLine("number = {0}", number);
TestWithoutRef(number);
Console.WriteLine("number = {0}", number);
int outNumber ;
TestOut(out outNumber);
Console.WriteLine("outNumber = {0}" , outNumber);
Console.ReadLine(); //Pause
}
private static void TestOut( out int nubmer )
{
nubmer = 1001;
}
private static void TestRef( ref int number )
{
number = number + 1;
}
private static void TestWithoutRef( int number )
{
number = number + 1;
Console.WriteLine("In TestWithout number = {0}", number);
}
}
}
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int number = 100;
Console.WriteLine("number = {0}",number);
TestRef(ref number);
Console.WriteLine("number = {0}", number);
TestWithoutRef(number);
Console.WriteLine("number = {0}", number);
int outNumber ;
TestOut(out outNumber);
Console.WriteLine("outNumber = {0}" , outNumber);
Console.ReadLine(); //Pause
}
private static void TestOut( out int nubmer )
{
nubmer = 1001;
}
private static void TestRef( ref int number )
{
number = number + 1;
}
private static void TestWithoutRef( int number )
{
number = number + 1;
Console.WriteLine("In TestWithout number = {0}", number);
}
}
}
重結果可以推論出 都是傳參考值
沒有留言:
張貼留言