ASP.NET-必须初始化隐式类型的局部变量

ASP.NET - Implicitly-typed local variables must be initialized
2020-09-18
  •  译文(汉语)
  •  原文(英语)

我在这里有此代码:

var directories;

if (filePath == "")
{
    directories = Directory.GetDirectories(filePath);
}
else
{
    directories = Directory.GetDirectories(myPath);
}

但是在定义变量时出现此错误:

隐式类型的局部变量必须初始化

为什么会出现此错误,我将如何解决它?

解决过程1

使用时var,directories除非在声明时进行初始化,否则编译器不知道其类型.如果稍后要初始化,则必须声明一个类型.

string[] directories;

if (filePath == "")
{
    directories = Directory.GetDirectories(filePath);
}
//etc
解决过程2

没有分配就不能使用var.赋值决定了变量的类型.只需更改以声明特定类型,就可以了.

解决过程3

编译器无法弄清楚是什么类型directories,因为您没有初始化它.

试试这个:

string[] directories;
解决过程4

您应该了解C#是强类型语言.因此,编译器无法确定directories编译时的内容.

var关键字是在编译时用于匿名类型绑定的.因此,如果不初始化directories它的值,则编译器将其未知,因为在运行时,实际的类型(值或引用)将被获取,var而在图中没有显示.

所以你可以尝试像

string[] directories;

if (filePath == "")
{
    directories = Directory.GetDirectories(filePath);
}

或者,如果您只想使用var,则可以这样使用它:

var directories = (string)null;

if (filePath == "")
{
    directories = Directory.GetDirectories(filePath);
}

您还可以参考MSDN以获得详细信息:隐式键入局部变量

I have this code Here:

var directories;

if (filePath == "")
{
    directories = Directory.GetDirectories(filePath);
}
else
{
    directories = Directory.GetDirectories(myPath);
}

But I get this error when defining the variable:

Implicitly-typed local variables must be initialized

Why am i getting this error and how would I go about fixing it?

Solutions1

When using var, the compiler doesn't know what the type of directories is unless you initialize while you declare it. You have to declare a type if you're initializing later.

string[] directories;

if (filePath == "")
{
    directories = Directory.GetDirectories(filePath);
}
//etc
Solutions2

You can't use var without an assignment. It is the assignment that determines the type of a var. Just change to declare a specific type and you should be good.

Solutions3

The compiler cannot figure out what type directories is because you did not initialize it.

Try this:

string[] directories;
Solutions4

You should understand that C# is strongly typed language. So compiler could not determine what directories is at compile time.

var keyword was introduced for anonymous type binding at compile time. So if you dont initilize the value of directories it is unknown to the compiler as at runtime the actual type(value or reference) is taken and var is nowhere in the picture.

So you can try like

string[] directories;

if (filePath == "")
{
    directories = Directory.GetDirectories(filePath);
}

or if you want to use var only then you can use it like this:

var directories = (string)null;

if (filePath == "")
{
    directories = Directory.GetDirectories(filePath);
}

You can also refer MSDN for details: Implicitly Typed Local Variables

转载于:https://stackoverflow.com/questions/32362032/asp-net-implicitly-typed-local-variables-must-be-initialized

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入