c# - Difference between null and not initialized? -


when write following code in c#:

sqlcecommand command; try {     // command used affect data     command = new sqlcecommand                   {                       // init code removed brevity                   };      // stuff      // more stuff } {     if (command != null)         command.dispose(); } 

resharper complains on check of command != null. says command may not assigned (because fail how in constructing , still hit try block).

so change declaration of command sqlcecommand command = null; , happy.

but left wondering difference is?

and why doesn't default null? meaning: how c# benefit not defaulting local variables null?

class field members defaulted (value types each depending on type, ref types null) local variables not.

// example 1: someclass someobject; if(x == y) {     someobject = new someclass(); } someobject.somemethod();   //error since may not execute if statements  // example 2 someclass someobject; someobject = new someclass(); someobject.somemethod();   //no error 

edit: designed avoid c++ runtime errors causing program close unexpectedly (because of null pointers).

p.s: may not downvote me when talk c# specs (i didn't make c# specs, cheer avoidance though).


Comments

Popular posts from this blog

Javascript line number mapping -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -