Skip to content

Instantly share code, notes, and snippets.

@cz75hiro
Created December 1, 2010 04:34
Show Gist options
  • Save cz75hiro/722954 to your computer and use it in GitHub Desktop.
Save cz75hiro/722954 to your computer and use it in GitHub Desktop.
参照型のパラメータの渡し方の違い
class Program{
static void Main(string[] args){
//参照型の値渡し
TestClass t1 = new TestClass();
TestClass t2 = ValMethod(t1);
Console.WriteLine(t1 == t2);//false
//参照型の参照渡し
TestClass t3 = new TestClass();
TestClass t4 = RefMethod(ref t3);
Console.WriteLine(t3 == t4);//true
Console.ReadLine();
}
static TestClass ValMethod(TestClass obj){
obj = new TestClass();
return obj;
}
static TestClass RefMethod(ref TestClass obj){
obj = new TestClass();
return obj;
}
class TestClass{
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment