}
//********************Delegate(Beispiel3)*****************
namespace SimpleDelegateSample {
public delegate int Calculate (int value1, int value2); //defining the delegate
public class MyClass //the class which contains the methods that will be assigned to delegate objects
public MyClass() { }
public int add(int value1, int value2) {
return value1 + value2;
}
public int sub( int value1, int value2) {
return value1 - value2;
static void Main(string[] args)
{
MyClass mc = new MyClass();
Calculate add = new Calculate(mc.add); //creating delegate objects and assigning appropriate methods Calculate sub = new Calculate(mc.sub);
//creating the class which contains the methods that will be assigned to delegate objects

Console.WriteLine("Adding two values: " + add(10, 6)); Console.WriteLine("Subtracting two values: " + sub(10,4)); Console.ReadLine();
}
//using the delegate objects to call the assigned methods
//********************Event*********************************
public delegate void MyHandler1(object sender,MyEventArgs e); //Step 1 Create delegate object public delegate void MyHandler2(object sender,MyEventArgs e);
//Step 2 Create event handler methods
public const string m_id="Class A";
public void OnHandler1(object sender,MyEventArgs e) {
Console.WriteLine("I am in OnHandler1 and MyEventArgs is {0}", e.m_id);
}
public void OnHandler2(object sender,MyEventArgs e) {
Console.WriteLine("I am in OnHandler2 and MyEventArgs is {0}", e.m_id);
}
public A(B b) //Step 3 create delegates, plug in the handler and register with the object that will fire the events {
MyHandler1 d1=new MyHandler1(OnHandler1); MyHandler2 d2=new MyHandler2(OnHandler2); b.Event1 +=d1;
b.Event2 +=d2;
//Step 4 Calls the encapsulated methods through the delegates (fires events)
public event MyHandler1 Event1; public event MyHandler2 Event2; public void FireEvent1(MyEventArgs e) {
if(Event1 != null) {
Event1(this,e); public void FireEvent2(MyEventArgs e)
public class MyEventArgs : EventArgs {
public string m_id;
}
public class Driver {
if(Event2 != null) {
Event2(this,e);
}
public static void Main() {
B b= new B();
A a= new A(b);
MyEventArgs e1=new MyEventArgs(); MyEventArgs e2=new MyEventArgs(); e1.m_id ="Event args for event 1"; e2.m_id ="Event args for event 2"; b.FireEvent1(e1);
b.FireEvent2(e2);
//*****************Event(Beispiel2)*************************************
public class MyEventArgs : EventArgs {
private string msg;
public MyEventArgs( string messageData ) {
msg = messageData; }
public string Message {
get { return msg; }
set { msg = value; } }
}
public class HasEvent {
public event EventHandler<MyEventArgs> SampleEvent;
public void DemoEvent(string val) {
EventHandler<MyEventArgs> temp = SampleEvent; if (temp != null)
temp(this, new MyEventArgs(val)); }
}
public class Sample {
public static void Main() {
// Declare an event of delegate type EventHandler of MyEventArgs.
// Copy to a temporary variable to be thread-safe.
HasEvent he = new HasEvent();
he.SampleEvent += new EventHandler<MyEventArgs>(SampleEventHandler); he.DemoEvent("Hey there, Bruce!");
he.DemoEvent("How are you today?");
}
private static void SampleEventHandler(object src, MyEventArgs mea) {
Console.WriteLine(mea.Message); }
} //***********************************************
//***************Threading******************************************************
Thread t = new Thread(new ThreadStart(DoWork)); //Methode->protected void DoWork() t.Start();...
mehr erfahren »
Fenster schließen
}
//********************Delegate(Beispiel3)*****************
namespace SimpleDelegateSample {
public delegate int Calculate (int value1, int value2); //defining the delegate
public class MyClass //the class which contains the methods that will be assigned to delegate objects
public MyClass() { }
public int add(int value1, int value2) {
return value1 + value2;
}
public int sub( int value1, int value2) {
return value1 - value2;
static void Main(string[] args)
{
MyClass mc = new MyClass();
Calculate add = new Calculate(mc.add); //creating delegate objects and assigning appropriate methods Calculate sub = new Calculate(mc.sub);
//creating the class which contains the methods that will be assigned to delegate objects

Console.WriteLine("Adding two values: " + add(10, 6)); Console.WriteLine("Subtracting two values: " + sub(10,4)); Console.ReadLine();
}
//using the delegate objects to call the assigned methods
//********************Event*********************************
public delegate void MyHandler1(object sender,MyEventArgs e); //Step 1 Create delegate object public delegate void MyHandler2(object sender,MyEventArgs e);
//Step 2 Create event handler methods
public const string m_id="Class A";
public void OnHandler1(object sender,MyEventArgs e) {
Console.WriteLine("I am in OnHandler1 and MyEventArgs is {0}", e.m_id);
}
public void OnHandler2(object sender,MyEventArgs e) {
Console.WriteLine("I am in OnHandler2 and MyEventArgs is {0}", e.m_id);
}
public A(B b) //Step 3 create delegates, plug in the handler and register with the object that will fire the events {
MyHandler1 d1=new MyHandler1(OnHandler1); MyHandler2 d2=new MyHandler2(OnHandler2); b.Event1 +=d1;
b.Event2 +=d2;
//Step 4 Calls the encapsulated methods through the delegates (fires events)
public event MyHandler1 Event1; public event MyHandler2 Event2; public void FireEvent1(MyEventArgs e) {
if(Event1 != null) {
Event1(this,e); public void FireEvent2(MyEventArgs e)
public class MyEventArgs : EventArgs {
public string m_id;
}
public class Driver {
if(Event2 != null) {
Event2(this,e);
}
public static void Main() {
B b= new B();
A a= new A(b);
MyEventArgs e1=new MyEventArgs(); MyEventArgs e2=new MyEventArgs(); e1.m_id ="Event args for event 1"; e2.m_id ="Event args for event 2"; b.FireEvent1(e1);
b.FireEvent2(e2);
//*****************Event(Beispiel2)*************************************
public class MyEventArgs : EventArgs {
private string msg;
public MyEventArgs( string messageData ) {
msg = messageData; }
public string Message {
get { return msg; }
set { msg = value; } }
}
public class HasEvent {
public event EventHandler<MyEventArgs> SampleEvent;
public void DemoEvent(string val) {
EventHandler<MyEventArgs> temp = SampleEvent; if (temp != null)
temp(this, new MyEventArgs(val)); }
}
public class Sample {
public static void Main() {
// Declare an event of delegate type EventHandler of MyEventArgs.
// Copy to a temporary variable to be thread-safe.
HasEvent he = new HasEvent();
he.SampleEvent += new EventHandler<MyEventArgs>(SampleEventHandler); he.DemoEvent("Hey there, Bruce!");
he.DemoEvent("How are you today?");
}
private static void SampleEventHandler(object src, MyEventArgs mea) {
Console.WriteLine(mea.Message); }
} //***********************************************