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

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

问题描述

我遇到以下错误:

使用未分配的局部变量.

Use of unassigned local variable.

代码:

int c;
for (int b = 1; b < 5; b++)
{
    c = b * 2;
}

推荐答案

替换

int c;

使用

int c = 0; //or some other initial value

出现此错误是因为编译器不知道该循环是否曾经执行过/为c分配了一个值.因此,它不允许您使用 Console.WriteLine(c);

The error appears because the compiler doesn't know if the loop is ever excecuted / a value is assigned to c. So it doesn't allow you to use it Console.WriteLine(c);

int c; // this works because the compiler knows there is a values assigned to c
c = 1;
Console.WriteLine(c);

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

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