-1

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?

Hazel へいぜる
  • 2,751
  • 1
  • 12
  • 44
  • 1
    [It's called an object initializer](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/how-to-initialize-objects-by-using-an-object-initializer) – Pranav Hosangadi Oct 20 '20 at 15:17
  • 3
    @PranavHosangadi that's not quite it, as I'm not specifying a type here and the resulting structure isn't defined anywhere but that snippet. – Hazel へいぜる Oct 20 '20 at 15:18
  • 3
    Ah, okay. Then it's an [anonymous type](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/anonymous-types). Wasn't sure your `object` wasn't just a [mre]-replacement for a type you'd defined – Pranav Hosangadi Oct 20 '20 at 15:19
  • @PranavHosangadi that's a solid point! :D Thanks for helping out! – Hazel へいぜる Oct 20 '20 at 15:21

2 Answers2

4

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}!");
Polygnome
  • 7,639
  • 2
  • 37
  • 57
-1

This is called an Anonymous Type.

From the docs:

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.

Collen
  • 359
  • 3
  • 10
  • Thank you! Feel free to flesh this out for future readers, but thank you so flipping much! It was killing me lol – Hazel へいぜる Oct 20 '20 at 15:19
  • 3
    Correct, but please add some explanation to your answer here, rather then relying purely on an external link (which might change or move in future) – ADyson Oct 20 '20 at 15:20
  • I second what @ADyson is recommending. – Hazel へいぜる Oct 20 '20 at 15:20
  • 3
    [Why is linking bad?](//meta.stackexchange.com/q/7515/174780) | [Are answers that just contain links elsewhere really “good answers”?](//meta.stackexchange.com/q/8231/174780) | [Your answer is in another castle: when is an answer not an answer?](//meta.stackexchange.com/q/225370/174780) – Pranav Hosangadi Oct 20 '20 at 15:25
  • Apologies, I've included the MSDN's summary incase the link goes bad. – Collen Oct 20 '20 at 15:29