没有类型的对象初始化时叫什么?

What is it called when an object is initialized without a type?

我只遇到过几次,还不是很了解,但我觉得有必要研究一下幕后到底发生了什么。

我知道它正在创建一个对象的新实例,但尚未指定类型。

var myObject = new {
    SomeProperty = "ABC",
    SomeOtherProperty = true
};

当您将 var 换成 objectdynamic 时,它也有效。但是,考虑到这一点,我真的找不到它的名字来研究它。我相信它被称为 pseudo-classpseudo-object,但我找不到任何关于它的实际文档。

我已经对该主题进行了多次 Google 搜索 (here's the latest), and even reviewed a few related SO posts (here's one about initialization),但我仍然没有找到答案。


这实际上叫什么?

这叫做Anonymous Type

来自文档:

Anonymous types provide a convenient way to encapsulate a set of read-only properties into a single object without having to explicitly define a type first. The type name is generated by the compiler and is not available at the source code level. The type of each property is inferred by the compiler.

它被称为一个anonymous type,它被直接实例化了。

Instantiation of anonymous types

To create an instance of an anonymous type, use the new operator and object initializer syntax:

var example = new { Greeting = "Hello", Name = "World" };
Console.WriteLine($"{example.Greeting}, {example.Name}!");