问题描述
为什么公共变量不能声明为 Var
键入允许动态的位置?
当我声明一个变量为Var类型,如下所示:
Why Public variables cannot be declared as Var
Type where as dynamic is allowed?
When i declared a variable as Var type as like the following:
public Var xVariable; // It says Type or namespace could not be found
,因为它允许声明为
where as it allows the the declaration as
public dynamic xVariable;
推荐答案
public var s = "hello";
public var t = GetListOfUnits();
仍然不会允许。
var
是一个隐式类型,它打算与Linq一起使用,其中返回值的类型可能与任何声明的类型(或者可能是未声明类型的集合) - 它不应该用于代替VB中的Dim之类的变量类型。
dynamic
变量是不同的:它们的类型在运行时被计算,在编译时没有特别检查。
would still not be allowed.var
is an implicit typing, which was intended to be used with Linq, where the type of a return value may not correspond to any declared type (or may be a collection of an undeclared type) - it isn't meant to be used instead of a variable type like Dim in VB is.dynamic
variables are different: the type of these is evaluated at run time, it is specifically not checked at compile time.
这篇关于为什么公共变量不能声明为Var Type的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!