C# error: Use of unassigned local variable

前端 未结 4 1899
庸人自扰
庸人自扰 2020-12-03 16:43

I\'m not sure why I\'m getting this error, but shouldn\'t this code compile, since I\'m already checking to see if queue is getting initialized?

public stat         


        
相关标签:
4条回答
  • 2020-12-03 17:19

    The compiler doesn't know that Environment.Exit() does not return. Why not just "return" from Main()?

    0 讨论(0)
  • 2020-12-03 17:23

    The compiler doesn't know that the Environment.Exit() is going to terminate the program; it just sees you executing a static method on a class. Just initialize queue to null when you declare it.

    Queue queue = null;
    
    0 讨论(0)
  • 2020-12-03 17:32

    The compiler only knows that the code is or isn't reachable if you use "return". Think of Environment.Exit() as a function that you call, and the compiler don't know that it will close the application.

    0 讨论(0)
  • 2020-12-03 17:33

    A couple of different ways to solve the problem:

    Just replace Environment.Exit with return. The compiler knows that return ends the method, but doesn't know that Environment.Exit does.

    static void Main(string[] args) {
        if(args.Length != 0) {
           if(Byte.TryParse(args[0], out maxSize))
             queue = new Queue(){MaxSize = maxSize};
           else
             return;
        } else {
           return;   
    }
    

    Of course, you can really only get away with that because you're using 0 as your exit code in all cases. Really, you should return an int instead of using Environment.Exit. For this particular case, this would be my preferred method

    static int Main(string[] args) {
        if(args.Length != 0) {
           if(Byte.TryParse(args[0], out maxSize))
             queue = new Queue(){MaxSize = maxSize};
           else
             return 1;
        } else {
           return 2;
        }
    }
    

    Initialize queue to null, which is really just a compiler trick that says "I'll figure out my own uninitialized variables, thank you very much". It's a useful trick, but I don't like it in this case - you have too many if branches to easily check that you're doing it properly. If you really wanted to do it this way, something like this would be clearer:

    static void Main(string[] args) {
      Byte maxSize;
      Queue queue = null;
    
      if(args.Length == 0 || !Byte.TryParse(args[0], out maxSize)) {
         Environment.Exit(0);
      }
      queue = new Queue(){MaxSize = maxSize};
    
      for(Byte j = 0; j < queue.MaxSize; j++)
        queue.Insert(j);
      for(Byte j = 0; j < queue.MaxSize; j++)
        Console.WriteLine(queue.Remove());
    }
    

    Add a return statement after Environment.Exit. Again, this is more of a compiler trick - but is slightly more legit IMO because it adds semantics for humans as well (though it'll keep you from that vaunted 100% code coverage)

    static void Main(String[] args) {
      if(args.Length != 0) {
         if(Byte.TryParse(args[0], out maxSize)) {
            queue = new Queue(){MaxSize = maxSize};
         } else {
            Environment.Exit(0);
            return;
         }
      } else { 
         Environment.Exit(0);
         return;
      }
    
      for(Byte j = 0; j < queue.MaxSize; j++)
         queue.Insert(j);
      for(Byte j = 0; j < queue.MaxSize; j++)
         Console.WriteLine(queue.Remove());
    }
    
    0 讨论(0)
提交回复
热议问题