-
-
Save iwadon/97ad4543ffeef1de940a8c6216907c7b to your computer and use it in GitHub Desktop.
C#のデリゲートの挙動を確認するためのコード。
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
using System; | |
class Base | |
{ | |
public int i; | |
public Action act; | |
} | |
class A : Base | |
{ | |
public A() | |
{ | |
i = 1; | |
} | |
public void Func() | |
{ | |
Console.WriteLine("A: i = " + i); | |
} | |
} | |
class B : Base | |
{ | |
public B() | |
{ | |
i = 2; | |
} | |
public void Func() | |
{ | |
Console.WriteLine("B: i = " + i); | |
} | |
} | |
class Program | |
{ | |
static void Main() | |
{ | |
A a = new A(); | |
B b = new B(); | |
a.act = a.Func; | |
a.act(); // => A: i = 1 | |
a.act = b.Func; | |
a.act(); // => B: i = 2 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
[雑記] デリゲートの内部 - C# によるプログラミング入門 | ++C++; // 未確認飛行 C
http://ufcpp.net/study/csharp/functional/miscdelegateinternal/
なるほど…