Skip to content

Instantly share code, notes, and snippets.

@iwadon
Last active May 23, 2018 11:12
Show Gist options
  • Save iwadon/97ad4543ffeef1de940a8c6216907c7b to your computer and use it in GitHub Desktop.
Save iwadon/97ad4543ffeef1de940a8c6216907c7b to your computer and use it in GitHub Desktop.
C#のデリゲートの挙動を確認するためのコード。
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
}
}
@iwadon
Copy link
Author

iwadon commented May 23, 2018

[雑記] デリゲートの内部 - C# によるプログラミング入門 | ++C++; // 未確認飛行 C
http://ufcpp.net/study/csharp/functional/miscdelegateinternal/

インスタンスと関数ポインターのペアです。

なるほど…

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment