Keyword event

Hi guys, I have been using C # and events lately, but I'm just starting to create my own events and use them. I got a little confused about why to use the event keyword, I got the same result only using delegates.

public partial class Form1 : Form { ServerConnection connection = new ServerConnection(); public Form1() { InitializeComponent(); connection.ref = new del(MethOne); connection.ref += new del(MethTwo); } public void MethOne(object message) { MessageBox.Show((string)message); } public void MethTwo(object message) { MessageBox.Show((string)message); } } public delegate void del(string message); public class ServerConnection { private TcpListener tcpListener; public del ref; private List<NetworkStream> clientList = new List<NetworkStream>(); public ServerConnection() { this.tcpListener = new TcpListener(IPAddress.Any, 3000); ThreadStart startListening = new ThreadStart(ListenForClients); Thread startThread = new Thread(startListening); startThread.Start(); } public void ListenForClients() { tcpListener.Start(); ParameterizedThreadStart handleClient = new ParameterizedThreadStart(HandleClient); while (true) { TcpClient newClient = tcpListener.AcceptTcpClient(); Thread handleClientThread = new Thread(handleClient); handleClientThread.Start(newClient); } } public void HandleClient(object newClient) { NetworkStream clientStream = ((TcpClient)newClient).GetStream(); clientList.Add(clientStream); BinaryFormatter formatter = new BinaryFormatter(); string message; while (true) { message = (string)formatter.Deserialize(clientStream); ref((string)message); } } 
+7
source share
5 answers

The event keyword allows you to specify add and remove operations inside a declaration line.

 private Action _myEvent; public event Action MyEvent { add { Console.WriteLine("Listener added!"); _myEvent += value; } remove { Console.WriteLine("Listener removed!"); _myEvent -= value; } } 
+5
source

Take a look

C # events against delegates

The event keyword is a modifier for declaring a delegate, which allows it to be included in the interface, restricts its call from in the class that declares it, provides it with a couple of custom accessors (add and remove) and forces the signature of the delegate (when used within the framework of the .NET platform) )

+8
source

The goal is to determine what the event is and what the callback is.

Both seem to be one and the same, but the meaning is different.

Visual Studio also places different icons to indicate events.

If I remember well, these are the first days of C #, delegates did not support this:

 this.mydelegatefield += somethingHere; 

Only events ... but maybe it's just my imagination.

EDIT

Just not to be skipped ... there is a difference in the add / remove methods. I put this after the other answers (since I forgot about it). So, the loan is not mine.

+3
source

The event is just a sugar cloak. 3 events occur when you define an event.

Simple EG:

 public event EventHandler alarm; 

Compiler Output Example

 private EventHandler alarm public void add_alarm(EventHandler value) { } public void remove_alarm(EventHandler value) { } 

Pay attention to private as opposed to public del me; Public accessors can cause problems. Also, using get and set is a better template.

0
source

events can be compared with the properties of your class.

  • Properties are the interfaces to your MemberField / Object states.
  • Similarly, an event is an interface to the base delegate.

you can still reach endresult without event. But you lose encapsulation without events.

An unprotected delegate may be subject to abuse.

0
source

All Articles