c#中的`new object()`和`new {}`之间有什么区别吗?

时间:2022-06-05 00:37:17

in c#,

var x = new {};

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

声明一个没有属性的匿名类型。这有什么不同吗?

var x = new object();

?

4 个解决方案

#1


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(); 

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.

如果你问新的{}是否有用 - 嗯,这是另一回事......我不能立即想到任何明智的用途。

#2


Well, for starters, object is an actual, non-anonymous type...if you do x.GetType() on the 2nd example, you'll get back System.Object.

好吧,对于初学者来说,object是一个实际的非匿名类型...如果你在第二个例子中执行x.GetType(),你将得到System.Object。

#3


Along with the return from GetType as mentioned, x would not be of type object, so you would not be able to assign an object type to that variable.

除了提到的GetType返回之外,x也不是object类型,因此您无法将对象类型分配给该变量。

        var x = new { };
        var y = new object();

        //x = y; //not allowed
        y = x; //allowed

#4


Jon Skeet's answer was mostly what I wanted, but for the sake of completeness here are some more differences, gained from reflector:

Jon Skeet的回答主要是我想要的,但为了完整起见,从反射器中获得了一些更多的差异:

new {} overrides three methods of object :

new {}会覆盖三个对象方法:

  1. Equals - as mentioned in other answers, new object and new {} have different types, so they are not equal.
  2. 等于 - 如其他答案中所提到的,新对象和新{}具有不同的类型,因此它们不相等。

  3. GetHashCode returns 0 for new {} (but why would you put it in a hash table anyway?)
  4. GetHashCode为new {}返回0(但为什么还要把它放在哈希表中?)

  5. ToString prints "{}" for new {}
  6. ToString为新{}打印“{}”

Unfortunately I can't think of a practical application for all this. I was just curious.

不幸的是,我无法想到这一切的实际应用。我只是好奇而已。

#1


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(); 

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.

如果你问新的{}是否有用 - 嗯,这是另一回事......我不能立即想到任何明智的用途。

#2


Well, for starters, object is an actual, non-anonymous type...if you do x.GetType() on the 2nd example, you'll get back System.Object.

好吧,对于初学者来说,object是一个实际的非匿名类型...如果你在第二个例子中执行x.GetType(),你将得到System.Object。

#3


Along with the return from GetType as mentioned, x would not be of type object, so you would not be able to assign an object type to that variable.

除了提到的GetType返回之外,x也不是object类型,因此您无法将对象类型分配给该变量。

        var x = new { };
        var y = new object();

        //x = y; //not allowed
        y = x; //allowed

#4


Jon Skeet's answer was mostly what I wanted, but for the sake of completeness here are some more differences, gained from reflector:

Jon Skeet的回答主要是我想要的,但为了完整起见,从反射器中获得了一些更多的差异:

new {} overrides three methods of object :

new {}会覆盖三个对象方法:

  1. Equals - as mentioned in other answers, new object and new {} have different types, so they are not equal.
  2. 等于 - 如其他答案中所提到的,新对象和新{}具有不同的类型,因此它们不相等。

  3. GetHashCode returns 0 for new {} (but why would you put it in a hash table anyway?)
  4. GetHashCode为new {}返回0(但为什么还要把它放在哈希表中?)

  5. ToString prints "{}" for new {}
  6. ToString为新{}打印“{}”

Unfortunately I can't think of a practical application for all this. I was just curious.

不幸的是,我无法想到这一切的实际应用。我只是好奇而已。