C# error: Use of unassigned local variable


 
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 static void Main(String[] args) { 
    Byte maxSize; 
    Queue queue; 
 
    if(args.Length != 0) 
    { 
        if(Byte.TryParse(args[0], out maxSize)) 
            queue = new Queue(){MaxSize = maxSize}; 
        else 
            Environment.Exit(0); 
    } 
    else 
    { 
        Environment.Exit(0); 
    } 
 
    for(Byte j = 0; j < queue.MaxSize; j++) 
        queue.Insert(j); 
    for(Byte j = 0; j < queue.MaxSize; j++) 
        Console.WriteLine(queue.Remove()); } 
So if queue is not initialized, then the for loops aren't reachable right? Since the program already terminates with Environment.Exit(0)?
Hope ya'll can give me some point


C# error: Use of unassigned local variable


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;



Comments