40 lines
1011 B
C#
40 lines
1011 B
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Test_Events
|
|
{
|
|
class MyClass
|
|
{
|
|
public int Result { set; get; }
|
|
|
|
public int Add (int x, int y)
|
|
{
|
|
int result = x + y;
|
|
|
|
// Das Event wird gefeuert wenn result ungleich 0 ist.
|
|
OnEvent(result);
|
|
|
|
return result;
|
|
}
|
|
|
|
//------------------------------------------------------------------------------------------------
|
|
// Der Delegat muß die gleiche Signatur aufweisen wie die Eventhandler-Methode.
|
|
public delegate void EventDelegate(int result);
|
|
|
|
// Das Event-Objekt ist vom Typ dieses Delegaten.
|
|
public event EventDelegate MyEvent;
|
|
|
|
public void OnEvent(int result)
|
|
{
|
|
// Prüft ob das Event überhaupt einen Abonnenten hat.
|
|
if (MyEvent != null)
|
|
{
|
|
MyEvent(result);
|
|
}
|
|
}
|
|
}
|
|
}
|