Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Event or Delegation

0 views
Skip to first unread message

Hartmut Fiebiger

unread,
Apr 18, 2002, 10:25:25 AM4/18/02
to
Hi

can anybody tell me what is the difference between the EVENT and DELEGATE
Keyword in C#?

Why do I ask?
I have the same "event" effekt, if I just use a standard delegation
mechanism (see code). So what for I nee the Keyword event?

Best regards
Hartmut
-------------------------------------------------
SouceSample for event or delegate:

using System;

public delegate void EventHandler(string strText);

class EventSource
{
// Event
public event EventHandler EreignisHandler;

// or a simple Delegate
//
// both works, just try it
// What is the difference?

// public EventHandler EreignisHandler;

public void Ereignis()
{
if(EreignisHandler!=null) EreignisHandler("Event wurde ausgelöst");
}
}

class TestApp
{
public static void Main()
{
EventSource eSrc=new EventSource ();
eSrc.EreignisHandler += new EventHandler (CatchEvent);
eSrc.Ereignis();

eSrc.EreignisHandler-= new EventHandler (CatchEvent);
eSrc.Ereignis ();

TestApp theApp=new TestApp ();
eSrc.EreignisHandler+= new EventHandler (theApp.InstanceCatch);
eSrc.Ereignis ();
}

public static void CatchEvent(string strText)
{
Console.WriteLine (strText);
}

public void InstanceCatch(string strText)
{
Console.WriteLine ("Instance:" + strText);
}
}


Nicholas Paldino [.NET/C# MVP]

unread,
Apr 18, 2002, 10:42:04 AM4/18/02
to
Hartmut,

A delegate is nothing more than a type-safe method definition. It is a
way of saying "this method that is encapsulated in the delegate has this
parameter list with this return value". That's it.

An event needs to know that what it is calling meets a certain criteria,
namely that the parameter list is defined a certain way and that the return
type is defined a certain way. Because of this, delegates are used to hook
up client objects with events.

Hope this helps.


--
- Nicholas Paldino [.NET MVP]
- nicholas...@exisconsulting.com

"Hartmut Fiebiger" <H.Fie...@HartSoft.de> wrote in message
news:OfNfiSu5BHA.1996@tkmsftngp05...

Hartmut Fiebiger

unread,
Apr 18, 2002, 11:13:34 AM4/18/02
to
Hi Nicholas

thx for your reply

I think I understood, that an delegates syntactically are used for events,
but I don't see any difference of what I can do with a delegate or an event.
Maybe have second look at my example, I got the same event effect with the
pure delegate (in comment)
Maybe Do you have an event example, where I really need the event keyword.

Best regards
hartmut


"Nicholas Paldino [.NET/C# MVP]" <nicholas...@exisconsulting.com>
schrieb im Newsbeitrag news:O058Vdu5BHA.2216@tkmsftngp04...

Eric Gunnerson [MS]

unread,
Apr 18, 2002, 2:27:51 PM4/18/02
to
Go here: http://www.gotdotnet.com/team/csharp/Information/ericgu_column.aspx

and find the columns on delegates and events (april and may of last year, I
think).

--
Visit the C# product team at http://www.gotdotnet.com/team/csharp

This posting is provided "AS IS" with no warranties, and confers no rights.

"Hartmut Fiebiger" <H.Fie...@HartSoft.de> wrote in message

news:eG34ctu5BHA.2292@tkmsftngp04...

Hartmut Fiebiger

unread,
Apr 18, 2002, 2:58:43 PM4/18/02
to
Hi Eric

thanks a lot for your reply!!!
That's the information I was looking for, to understand the different from
event and a normal delegate.

You're doing a good job !!! ;-)

Hartmut


"Eric Gunnerson [MS]" <ericgu...@microsoft.spam=no.com> schrieb im
Newsbeitrag news:ODal8aw5BHA.1636@tkmsftngp07...

0 new messages