using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Event_training
{
class Publisher
{
public event EventHandler x;
public void raise()
{
x(this, null);
}
}
class Subscriber
{
public void method1(object o, EventArgs e)
{
Console.WriteLine("metod1 called");
}
public void method2(object o, EventArgs e)
{
Console.WriteLine("metod2 called");
}
}
class Program
{
static void Main(string[] args)
{
Publisher p = new Publisher();
Subscriber s = new Subscriber();
p.x += s.method1;
p.x += s.method2;
p.raise();
}
}
}
難以下傳「this」關鍵字。它是指什麼「x(this,null);」在這裏?我可以使用別的東西而不是「這個」嗎?在這段代碼中,我可以使用別的東西而不是「this」嗎?
請閱讀[此(C#參考)(https://msdn.microsoft.com/en-us/library/dk1507sz.aspx)和關閉問題 –
'this'是指'發佈者'的當前實例。 –
我爲什麼要將它的實例提交給它的方法? – Lyrk