C#中的var vs Object [英] var vs Object in C#

查看:28
本文介绍了C#中的var vs Object的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
"var"之间的差异和对象"在C#

我想知道var和object之间的区别.

I would like to know the difference between var and object.

何时使用Var和何时使用Object.

When to use Var and when to use Object.

使用它们的优缺点.

推荐答案

var 只是让编译器为我选择正确的变量类型"的简写(编译时类型推断是更准确的术语).

var is just shorthand for "let the compiler pick the right variable type for me" (compile-time type-inference is the more exact term).

object 是一种特定的类型.所有其他引用类型都源自 object ,因此您可以将任何内容分配给 object 类型的变量.

object, on the other hand, is a specific type; all other reference types derive from object, so you can assign anything to a variable of type object.

这篇关于C#中的var vs Object的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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