C#错误:未分配的局部变量使用 [英] C# error: Use of unassigned local variable

查看:121
本文介绍了C#错误:未分配的局部变量使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道为什么我得到这个错误,但不应该这样code编译,因为我已经检查,看看是否队列得到初始化?

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());
}

因此​​,如果队列中没有初始化,那么for循环无法到达正确的?由于该方案已经与Environment.Exit终止(0)?

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 pointers :)

感谢。

推荐答案

编译器不知道Environment.Exit()将要终止程序;它只是看到你在执行一个类的静态方法。只是初始化队列来,当你把它声明无效。

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;

这篇关于C#错误:未分配的局部变量使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆