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

174 Views Asked by At

I've only encountered this a handful of times, and I don't understand it entirely yet, but I feel the need to research what's actually going on here behind the scenes.

I recognize that it is creating a new instance of an object, but the type has not been specified.

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

It also works when you swap out var for object or dynamic. However, with that in mind, I can't really find what it's called in order to research it. I believe it is referred to as a pseudo-class or pseudo-object but I can't find any actual documentation on it.

I've performed several Google searches on the topic (here's the latest), and even reviewed a few related SO posts (here's one about initialization), but I'm still not finding the answer.


What is this actually called?

1

There are 1 best solutions below

0
On BEST ANSWER

Its called an anonymous type, and its instantiated directly.

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}!");