質問
いいという名前のクラスにカエルで次のように記述されています。
public class Frog
{
public int Location { get; set; }
public int JumpCount { get; set; }
public void OnJump()
{
JumpCount++;
}
}
まず前提として、2つのもの
- したいイベントを生成するための名前のクラス定義で設定します。
- したいインスタンスを生成し、カエルのクラスを作成してもう一つの方法とされたときに呼び出されるカエルジャンプの様子。
解決
public event EventHandler Jump;
public void OnJump()
{
EventHandler handler = Jump;
if (null != handler) handler(this, EventArgs.Empty);
}
その
Frog frog = new Frog();
frog.Jump += new EventHandler(yourMethod);
private void yourMethod(object s, EventArgs e)
{
Console.WriteLine("Frog has Jumped!");
}
他のヒント
@CQ:なぜ自分の好きな名前を付けてコピー pf Jump
?また、保存することができますその後の試験を少しずつ変える宣言イベント:
public event EventHandler Jump = delegate { };
public void OnJump()
{
Jump(this, EventArgs.Empty);
}
こちらのログインidとパスワードの使い方を通常のEventHandler、またはカスタムに限ります。ご注意 ?.
が使用されているの .
めた場合には、イベントがnullの場合は、まず綺麗に(nullを返します)
public delegate void MyAwesomeEventHandler(int rawr);
public event MyAwesomeEventHandler AwesomeJump;
public event EventHandler Jump;
public void OnJump()
{
AwesomeJump?.Invoke(42);
Jump?.Invoke(this, EventArgs.Empty);
}
このイベントがnullの場合がない場合は、契約者は、一度呼び出されたその場で決済までオンラインでスレッドで安全です。でも割り当てデフォルトの空のハンドラを保障するためのイベントはnullです。これは技術的に脆弱なかったのですが、自分以外の誰か拭すべてのイベントを使用GetInvocationList)で使用の時は注意を怠らないこと。
public event EventHandler Jump = delegate { };
public void OnJump()
{
Jump(this, EventArgs.Empty);
}
所属していません StackOverflow