我正在学习C#,但是突然间,在C#Express 2008版中,编译器说即使使用最简单的代码,也要使用未分配的局部变量'a'。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int a;
a++;
Console.WriteLine(a);

}
}
}

那是一个错误。

最佳答案

您必须先初始化a变量(将其分配给它),然后才能使用它。基本上,编译器不知道什么是变量的起始值。在这种情况下,它不知道a的值,因此您必须为其添加一些内容:

int a = 0;
a++;

10-04 23:45
查看更多