none
Use of unassigned local variable

    Question

  •         protected
     bool
     IsLazyLoaded()
            {
                bool
     isLazyLoaded;
    
                switch
     (Page.ProductType)
                {
                    case
     ProductType.SearchResults:
                        isLazyLoaded = true
    ;
                        break
    ;
                    case
     ProductType.Store:
                        isLazyLoaded = true
    ;
                        break
    ;
                }
    
                return
     isLazyLoaded;
            }
    

    I mean I can set it initially to false, but wouldn't that defeat the point of having default values assigned by the compiler?
    Ok, so if a boolean is set to false by the compiler by default why would I still get this error:


    C# Web Developer
    • Edited by NoEgo Wednesday, August 19, 2009 6:31 PM
    Wednesday, August 19, 2009 6:30 PM

Answers

All replies

  • It's not required to be set when the variable is class or struct level, but when it's method level it is required to be set.  Even though the default is "technically" false, the declaration is required per C# language spec.  Could it have been another way?  Yes, but it's there to ensure you initialize your variables, as explicitly initializing them is good practice anyways.
    Coding Light - Illuminated Ideas and Algorithms in Software
    Coding Light WikiTwitterLinkedInForumsBrowser
    • Marked as answer by NoEgo Wednesday, August 19, 2009 6:44 PM
    Wednesday, August 19, 2009 6:32 PM
    Moderator
  • Thanks, cause I could never tell why, no I know when you need to set it and why.  I think it sucks though.  I like the default way to go and in this case I like the "free".  But I guess I have to use convention with local variables...errr
    C# Web Developer
    Thursday, August 20, 2009 12:32 AM