Note that there are some explanatory texts on larger screens.

plurals
  1. POPurpose of "event" keyword
    text
    copied!<p>Hey guys I've been using c# and events alot lately but I'm just starting to create my own events and use them. I'm a little confused on why to use the event keyword, I got the same result by only using delegates. </p> <pre><code>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&lt;NetworkStream&gt; clientList = new List&lt;NetworkStream&gt;(); 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); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload