C# 错误:使用未分配的局部变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/256073/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 20:12:16  来源:igfitidea点击:

C# error: Use of unassigned local variable

c#initialization

提问by jkidv

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)?

因此,如果队列未初始化,则无法访问 for 循环,对吗?由于程序已经以 Environment.Exit(0) 终止?

Hope ya'll can give me some pointers :)

希望你们能给我一些指点:)

Thanks.

谢谢。

采纳答案by tvanfosson

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 queueto null when you declare it.

编译器不知道 Environment.Exit() 将终止程序;它只是看到您在类上执行静态方法。只需queue在声明时初始化为 null 即可。

Queue queue = null;

回答by Brian

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

编译器不知道 Environment.Exit() 不会返回。为什么不直接从 Main() 中“返回”?

回答by Nelson Reis

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.

如果您使用“return”,编译器只知道代码是否可达。将 Environment.Exit() 视为您调用的函数,编译器不知道它将关闭应用程序。

回答by Mark Brackett

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.

只需用 return 替换 Environment.Exit 即可。编译器知道 return 结束方法,但不知道 Environment.Exit 这样做。

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

当然,您真的只能侥幸逃脱,因为您在所有情况下都使用 0 作为退出代码。真的,您应该返回一个 int 而不是使用 Environment.Exit。对于这种特殊情况,这将是我的首选方法

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 reallywanted to do it this way, something like this would be clearer:

将队列初始化为空,这实际上只是一个编译器技巧,上面写着“我会找出自己未初始化的变量,非常感谢”。这是一个有用的技巧,但在这种情况下我不喜欢它——你有太多的 if 分支来轻松检查你是否做得正确。如果你真的想这样做,这样的事情会更清楚:

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)

在 Environment.Exit 后添加 return 语句。同样,这更像是一个编译器技巧——但在 IMO 上稍微更合法,因为它也为人类增加了语义(尽管它会让你远离吹嘘的 100% 代码覆盖率)

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