var foo = new Love(); 和有什么区别AND 对象 foo = new Love();?

发布于 2024-09-17 16:58:15 字数 145 浏览 5 评论 0 原文

因为我不熟悉隐式类型;你能告诉我以下之间的主要区别吗:

 var foo = new Love(); 

object foo = new Love();

As I am not familiar with implicit typing; can you please tell me the main differences between:

 var foo = new Love(); 

AND

object foo = new Love();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

奢望 2024-09-24 16:58:15

在第一种情况下,foo 的类型是Love。在第二种情况下,它是对象

In the first case the type of foo is Love. In the second case it is object.

ゝ杯具 2024-09-24 16:58:15
var foo = new Love(); 

这里,变量foo静态类型是Love。它相当于编写 Love foo = new Love();

object foo = new Love();

这里,变量foo静态类型是object。如果不先使用强制转换,您就无法访问 Love 的任何方法。

在这两种情况下,foo动态类型(或运行时类型)都是Love,这就是为什么 GetType 将为两者返回 Love

var foo = new Love(); 

Here, the static type of variable foo is Love. It's equivalent to writing Love foo = new Love();.

object foo = new Love();

Here, the static type of variable foo is object. You cannot access any of Love's methods without using a cast first.

The dynamic type (or runtime type) of foo is Love in both cases, which is why GetType will return Love for both.

夏了南城 2024-09-24 16:58:15

使用var,编译器根据赋值运算符右侧的表达式推断变量的类型。

换句话说,

var foo = new Love();

完全相当于

Love foo = new Love();

所以Love的所有成员都可以通过foo获得 - 而如果foo > 被声明为 object 类型,您只能访问 GetHashCode()ToString()GetType( )Equals()

使用 var,您仍然使用静态类型(而不是在 C# 4 中使用动态)。您只是没有明确说明变量的类型 - 您让编译器为您解决它。但是,它确实需要是编译器可以计算出的类型。例如,这些都是无效的:

// All invalid
var a = null;
var b = delegate() { Console.WriteLine("Hello"); };
var c = x => Console.WriteLine("lambda: " + x);
var d = MethodName; // Attempted method group conversion

在这些情况下,编译器没有足够的信息来确定您指的是哪种类型。

With var, the compile infers the type of the variable based on the expression on the right-hand side of the assignment operator.

In other words,

var foo = new Love();

is exactly equivalent to

Love foo = new Love();

So all the members of Love will be available via foo - whereas if foo were declared to be of type object, you'd only have access to GetHashCode(), ToString(), GetType() and Equals().

With var, you're still using static typing (as opposed to using dynamic in C# 4). You're just not explicitly stating the type of the variable - you're letting the compiler work it out for you. However, it does need to be a type that the compiler can work out. So for example, these are all invalid:

// All invalid
var a = null;
var b = delegate() { Console.WriteLine("Hello"); };
var c = x => Console.WriteLine("lambda: " + x);
var d = MethodName; // Attempted method group conversion

In these cases the compiler doesn't have enough information to work out which type you mean.

故事↓在人 2024-09-24 16:58:15
var foo = new Love();

这里它等于

Love foo = new Love();

有时我们可以使用 var 来避免长类名,例如 Dictionary>。 您可以使用 的所有方法/属性类爱。

object foo = new Love();

现在 foo 被认为是一个对象,你看不到 Love 类的任何方法/属性,但你可以将它转换回来。

var foo = new Love();

here it equals to

Love foo = new Love();

sometimes we can use var to avoid long class name e.g. Dictionary<string,Dictionary<int,string>>. You can use all methods/properties of class Love.

object foo = new Love();

Now foo is considered to be an object, you can't see any method/property of class Love, but you can convert it back.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文