如果要將對象傳遞給方法,為什么要使用ref關鍵字?這不是默認的行為嗎?例如:class Program{
static void Main(string[] args)
{
TestRef t = new TestRef();
t.Something = "Foo";
DoSomething(t);
Console.WriteLine(t.Something);
}
static public void DoSomething(TestRef t)
{
t.Something = "Bar";
}}public class TestRef{
public string Something { get; set; }}輸出為“Bar”,這意味著將對象作為引用傳遞。為什么在傳遞對象時使用‘ref’關鍵字?
- 3 回答
- 0 關注
- 739 瀏覽
添加回答
舉報
0/150
提交
取消