简体   繁体   English

使用未分配的局部变量“ a”

[英]Use of unassigned local variable 'a'

I am Learning c#, but suddenly the compiler in c# express edition 2008 says Use of unassigned local variable 'a', even with the simplest codes. 我正在学习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);

}
}
}

That one gave an error. 那是一个错误。

You have to initialize (assign something to) your a variable before you can use it. 您必须先初始化变量( a变量赋值),然后才能使用它。 Basically compiler doesn't know what is a starting value of a variable. 基本上,编译器不知道什么是变量的起始值 In this case it doesn't know the value of a , you have to assing something to it: 在这种情况下,不知道价值a ,你必须assing东西吧:

int a = 0;
a++;

Before Initializing the value to the variable you cannot increment or decrement it. 在将值初始化为变量之前,不能对其进行递增或递减。 so initialize it. 所以初始化它。 The use it. 使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM