将 C# 中“var”的值初始化为 null

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17417647/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 09:26:42  来源:igfitidea点击:

Initialize value of 'var' in C# to null

c#var

提问by Nikhil Chavan

I want to assign a variable to an initial value of null, and assign its value in the next if-elseblock, but the compiler is giving an error,

我想将一个变量分配给一个初始值 null,并在下一个if-else块中分配它的值,但是编译器给出了一个错误,

Implicitly-typed local variables must be initialized.

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

How could I achieve this?

我怎么能做到这一点?

采纳答案by user2246674

varvariables still have a type - and the compiler error message says this type must be established during the declaration.

var变量仍然有一个类型 - 并且编译器错误消息说必须在声明期间建立这个类型。

The specific request (assigning an initial null value) can be done, but I don't recommend it. It doesn't provide an advantage here (as the type must still be specified) and it could be viewed as making the code less readable:

具体的请求(分配一个初始空值)是可以的,但是我不推荐。它在这里没有提供优势(因为仍然必须指定类型),并且可以将其视为使代码可读性降低:

var x = (String)null;

Which is still "type inferred" and equivalent to:

这仍然是“类型推断”,相当于:

String x = null;

The compiler will notaccept var x = nullbecause it doesn't associate the null with any type - not even Object. Using the above approach, var x = (Object)nullwould "work" although it is of questionable usefulness.

编译器不会接受,var x = null因为它不会将 null 与任何类型相关联——甚至是 Object。使用上述方法var x = (Object)null可以“工作”,尽管它的用处值得怀疑。

Generally, when I can't use var's type inference correctly then

通常,当我无法var正确使用的类型推断时

  1. I am at a place where it's best to declare the variable explicitly; or
  2. I should rewrite the code such that a validvalue (with an established type) is assigned during the declaration.
  1. 我在一个最好显式声明变量的地方;或者
  2. 我应该重写代码,以便在声明期间分配一个有效值(具有已建立的类型)。

The second approach can be done by moving code into methods or functions.

第二种方法可以通过将代码移动到方法或函数中来完成。

回答by Steven Wexler

The varkeyword in C#'s main benefit is to enhance readability, not functionality. Technically, the varkeywords allows for some other unlocks (e.g. use of anonymous objects), but that seems to be outside the scope of this question. Every variable declared with the varkeyword has a type. For instance, you'll find that the following code outputs "String".

varC# 中的关键字的主要好处是增强可读性,而不是功能。从技术上讲,var关键字允许其他一些解锁(例如使用匿名对象),但这似乎超出了本问题的范围。每个用var关键字声明的变量都有一个类型。例如,您会发现以下代码输出“String”。

var myString = "";
Console.Write(myString.GetType().Name);

Furthermore, the code above is equivalent to:

此外,上面的代码等价于:

String myString = "";
Console.Write(myString.GetType().Name);

The varkeyword is simply C#'s way of saying "I can figure out the type for myStringfrom the context, so don't worry about specifying the type."

var关键字是简单的C#的的方式说:‘我可以计算出类型myString从上下文,所以不用担心指定的类型。’

var myVariable = (MyType)nullor MyType myVariable = nullshould work because you are giving the C# compiler context to figure out what type myVariableshould will be.

var myVariable = (MyType)null或者MyType myVariable = null应该可以工作,因为您正在提供 C# 编译器上下文来确定myVariable应该是什么类型。

For more information:

想要查询更多的信息: