在c#中,`new object()`和`new {}`之间有什么区别吗? [英] Is there any difference between `new object()` and `new {}` in c#?

查看:140
本文介绍了在c#中,`new object()`和`new {}`之间有什么区别吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在c#中,

var x = new {};

声明一个没有属性的匿名类型.这和

declares an anonymous type with no properties. Is this any different from

var x = new object();

?

推荐答案

是的,使用的类型不同.您可以在编译时告诉这一点:

Yes, the types used are different. You can tell this at compile-time:

var x = new {};
// Won't compile - no implicit conversion from object to the anonymous type
x = new object(); 

如果您要问new{}是否有用过 -那是另一回事...我无法立即想到它的任何合理用途.

If you're asking whether new{} is ever useful - well, that's a different matter... I can't immediately think of any sensible uses for it.

这篇关于在c#中,`new object()`和`new {}`之间有什么区别吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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