Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy unused event arguments require assignment?
    primarykey
    data
    text
    <p>According to <a href="http://msdn.microsoft.com/en-us/library/edakx9da.aspx" rel="nofollow">C# Reference</a> </p> <blockquote> <p>"The null keyword is a literal that represents a null reference, one that does not refer to any object. null is the default value of reference-type variables'</p> </blockquote> <p>I was surprised to find out that<br> Commenting <code>e=null</code> line in the following app code (taken from the article <a href="http://softwarecafeblog.blogspot.ru/2012/06/difference-between-events-and-delegates.html" rel="nofollow">"Difference Between Events And Delegates in C#"</a>) results in compilation error: </p> <pre><code>Use of unassigned local variable 'e' </code></pre> <p>while without commenting it is compiled and run. </p> <p>I do not get: </p> <ul> <li>Where is variable <code>e</code> used? </li> <li>Is it possible to force the app to run without the dumb assigning the variable to <code>null</code>? </li> </ul> <p>f </p> <pre><code>using System; class Program { static void Main(string[] args) { DelegatesAndEvents obj = new DelegatesAndEvents(); obj.Execute(); } } public class DelegatesAndEvents { public event EventHandler MyEvent; internal void Execute() { EventArgs e; //Commenting the next line results in compilation error //Error 1 Use of unassigned local variable 'e' e = null; int sender = 15; MyEvent += MyMethod; MyEvent += MyMethod2; MyEvent(sender, e); } void MyMethod(object sender, EventArgs e) { Console.WriteLine(sender); } void MyMethod2(object sender, EventArgs e) { Console.WriteLine(sender); Console.ReadLine(); } } </code></pre> <p><strong>Update (or the comment to all answers):</strong><br> So, and I never knew it, there are to kind of nulls - one which is assigned and another which is not assigned... Funny... </p> <p>They should probably have different types, for checking:<br> if typeof(unassigned-null) then do this;<br> if typeof(assigned_null) then do that; </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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