例:
using System;
namespace ConsoleApplication1
{
class C
{
public static void reffun(ref string str)
{
str += " fun";
}
public static void outfun(out string str)
{
str = "test"; //必须在函数体内初始, 如无此句,则下句无法执行,报错
str += " fun";
}
}
class Class1
{
[STAThread]
static void Main(string[] args)
{
string test1 = "test";
string test2; //没有初始
C.reffun( ref test1 ); //正确
C.reffun( ref test2 ); //错误,没有赋值使用了test2
C.outfun( out test1 ); //正确,但值test无法传进去
C.outfun( out test2 ); //正确
Console.Read();
}
}
} |