c# - Use of local unassigned variable - even with else-statement -


this question has answer here:

mediadescription media; foreach(var field in fields) {     switch(field.key) {         case fieldtype.version:             message.version = convert.toint32(field.value);                  break;         case fieldtype.originator:             message.originator = originatorfield.parse(field.value);             break;         ...         ...         case fieldtype.information:             if(media == null)    <-- use of local unassigned variable                 message.description = field.value;             else media.description = field.value;             break;         ... 

i mean, why? compiler should smart enough precheck declaration , in else-statement declaration gets accessed. what's wrong?

being not assigned , being assigned null 2 different states of local variable. local variables have initialized something, null, before can accessed. default not initialized @ all, unlike class fields.

for comparison, code not give compilation error:

mediadescription media = null; ...         case fieldtype.information:         if(media == null)    <-- use of local unassigned variable             message.description = field.value; 

Comments

Popular posts from this blog

curl - PHP fsockopen help required -

HTTP/1.0 407 Proxy Authentication Required PHP -

java - More than one row with the given identifier was found: 1, for class: com.model.Diagnosis -