Created
December 1, 2010 04:34
-
-
Save cz75hiro/722954 to your computer and use it in GitHub Desktop.
参照型のパラメータの渡し方の違い
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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