Cadrick Loh
Cadrick Loh

Reputation: 721

C# terminology help needed

Recently I ran into this code but I don't know the terminology so I couldn't search on Google to learn more about this coding style.

Here's the code:

SomeObject someObject = new SomeObject()
{
   Name = "name",
   Value = 10
};

Does anyone know what is this called in C#?

Upvotes: 0

Views: 53

Answers (3)

Timothy Shields
Timothy Shields

Reputation: 79631

It's called initializer syntax and it is essentially doing the same thing as the following, but in a single expression:

SomeObject someObject = new SomeObject();
someObject.Name = "name";
someObject.Value = 10;

Upvotes: 3

Tommy
Tommy

Reputation: 39827

I believe what you are looking for is called an Object Initializer

From the MSDN:

You can use object initializers to initialize type objects in a declarative manner without explicitly invoking a constructor for the type.

Upvotes: 0

Saleem
Saleem

Reputation: 9018

This is called Object initializer . See documentation for Object initializers

Upvotes: 1

Related Questions