Created
May 8, 2019 10:08
-
-
Save yuchiki/0b08375bcf2cea240b78586c86430d9b to your computer and use it in GitHub Desktop.
同クラスの他インスタンスのprivateフィールドにアクセスできる例
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
namespace fuga | |
{ | |
class MyClass { | |
int value; //privateなフィールド | |
public int GetValue(MyClass mc) => mc.value; // 同クラス他インスタンスのprivateフィールドにアクセスできる!! | |
public MyClass(int v) => value = v; | |
} | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var class1 = new MyClass(1); | |
var class2 = new MyClass(2); | |
Console.WriteLine(class1.GetValue(class2)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment