Sunday, 15 February 2015

c# - Why unused event arguments require assignment? -



c# - Why unused event arguments require assignment? -

according c# reference

"the null keyword literal represents null reference, 1 not refer object. null default value of reference-type variables'

i surprised find out that commenting e=null line in next app code (taken article "difference between events , delegates in c#") results in compilation error:

use of unassigned local variable 'e'

while without commenting compiled , run.

i not get:

where variable e used? is possible forcefulness app run without dumb assigning variable null?

f

using system; class programme { static void main(string[] args) { delegatesandevents obj = new delegatesandevents(); obj.execute(); } } public class delegatesandevents { public event eventhandler myevent; internal void execute() { eventargs e; //commenting next line results in compilation error //error 1 utilize 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(); } }

update (or comment answers): so, , never knew it, there kind of nulls - 1 assigned , not assigned... funny...

they should have different types, checking: if typeof(unassigned-null) this; if typeof(assigned_null) that;

local variables not initialized default value, whereas fields are.

c# events delegates event-handling arguments

No comments:

Post a Comment