“var"和“var"之间的区别和“对象"在 C# 中 [英] Difference between "var" and "object" in C#
问题描述
var
类型是否等同于 VB 中的 Variant
?当 object
可以接受任何数据类型时,这两者有什么区别?
Is the var
type an equivalent to Variant
in VB? When object
can accept any datatype, what is the difference between those two?
推荐答案
从 Visual C# 3.0 开始,在方法范围内声明的变量可以具有隐式类型 var
.隐式类型的局部变量是强类型的,就像您自己声明了类型一样,但编译器会确定类型.i
的以下两个声明在功能上是等效的:
Beginning in Visual C# 3.0, variables that are declared at method scope can have an implicit type var
. An implicitly typed local variable is strongly typed just as if you had declared the type yourself, but the compiler determines the type. The following two declarations of i
are functionally equivalent:
var i = 10; //implicitly typed
int i = 10; //explicitly typed
var 不是对象
您绝对应该阅读此内容:C# 3.0 -Var 不是对象
You should definitely read this : C# 3.0 - Var Isn't Object
这篇关于“var"和“var"之间的区别和“对象"在 C# 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!