【问题标题】:Why does use of unassigned local variable occur? [closed]为什么会使用未分配的局部变量? [关闭]
【发布时间】:2015-06-30 16:34:40
【问题描述】:

这里有很多类似的问题,但我不明白为什么我不能在 C# 中使用它们,而是在其他语言中使用它们。如果我不使用 try catch 块但希望我在使用它时初始化变量,这段代码是如何工作的。在这种情况下如何进行内存分配。 附言我是初学者,其中一些对我来说意义有限。

using System;

public class Example
{
   public static void Main()
    {
    int user=0, pass=0;
    do
    {
        Console.WriteLine("Enter the username");
        try
        {
            user = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("Enter the password");
            pass = Convert.ToInt32(Console.ReadLine());
            if (user == 12 && pass == 1234)
                Console.WriteLine("Correct");
            else
                Console.WriteLine("Invalid Username or Password");
            Console.ReadKey();
        }
        catch (FormatException)
        {
            Console.WriteLine("Invalid Format");
        }
        catch (OverflowException)
        {
            Console.WriteLine("Overflow");
        }

    }
    while (user != 12 && pass != 1234);

}



    }

【问题讨论】:

  • 值得注意的是,“为什么我不能在 c# 中使用它们,但在其他语言中 [我可以]”回答为“因为 c# 是一种不同的语言”。这种行为是 c# 设计理念的一部分。他们可以很容易地让它在没有明确初始化的情况下工作,但他们没有。回答为什么实际上不在堆栈溢出的范围内,只是说他们认为这是一个比替代方案更好的主意。
  • This 问题解释了一些。

标签: c#


【解决方案1】:

在 C# 中,需要在访问变量之前对其进行初始化。在您的示例中,如果您没有 try/catch 块,变量 userpass 将被初始化为

    user = Convert.ToInt32(Console.ReadLine());

    pass = Convert.ToInt32(Console.ReadLine());

在您访问它们的行之前

    while (user != 12 && pass != 1234);

但是,如果您使用 try/catch 块,如您的示例中那样,并且 FormatExceptionOverflowException 被抛出

    Convert.ToInt32(Console.ReadLine());

那么一旦你在其中访问变量,它们就会被取消初始化

    while (user != 12 && pass != 1234);

【讨论】:

  • 变量在方法int user=0, pass=0;的第一行初始化
  • @Habib:问题是他为什么要初始化它们。所以问题是为什么他需要那条线,而没有 try catch 就不需要。这个答案似乎非常准确地回答了这个问题。
  • @Chris,你是对的,downvote 被删除了。
  • 谢谢。有道理!!
【解决方案2】:

因为如果你在 try/catch 之前没有初始化变量。您必须在每个块中指定它们

        int user, pass;
        do
        {
            Console.WriteLine("Enter the username");
            try
            {
                user = Convert.ToInt32(Console.ReadLine());// it will not be initialized if process fails.
                pass = Convert.ToInt32(Console.ReadLine());// it will not be initialized if process fails.
            }
            catch (FormatException)
            {
                // what is user and pass?
                Console.WriteLine("Invalid Format");
            }
            catch (OverflowException)
            {
                // what is user and pass?
                Console.WriteLine("Overflow");
            }

        } while (user != 12 && pass != 1234);

因为每个区块都有失败的可能性。所以你必须在每个块中初始化它们如果你在尝试捕获之前不这样做。

如果你不使用 try catch:

        int user, pass;
        do
        {
            Console.WriteLine("Enter the username");
            user = Convert.ToInt32(Console.ReadLine()); // it will be initialized any way.
            pass = Convert.ToInt32(Console.ReadLine()); // it will be initialized any way.


        } while (user != 12 && pass != 1234);

Convert.ToInt32(Console.ReadLine()); 那么 user 或 pass 的值是多少?

【讨论】:

  • 如果已经在封闭块中初始化,则无需在每个代码路径中进行初始化。
  • 我知道。我正在向用户说明。他问如果我使用 try catch 为什么需要初始化它?如果您不使用 try catch,则不需要在封闭块中进行初始化。 @Sehnsucht
  • 我不明白女巫部分我错了吗? @Sehnsucht
  • 没有什么是“错误的”,但您可能应该更清楚地说明任何变量都需要在访问之前分配一个值使用任何代码路径。这与 try/catch 关系不大;如果 do/while 循环以初始化变量的 if 块开始,则会出现相同的问题,因为在我们不输入的代码路径中,如果我们到达变量不会被初始化的点。跨度>
  • 是的,一般的答案是每个变量都必须在将其值用于任何目的之前进行初始化。 if/else 条件也会发生同样的事情.. switch case 等... @Sehnsucht
猜你喜欢
  • 2011-09-06
  • 1970-01-01
  • 2012-03-03
  • 1970-01-01
  • 2011-12-09
  • 2022-01-19
相关资源
最近更新 更多