Hi,

I came across this code snippet:

new { genre = genreName }

I am not sure what is the name of this thing and how this thing works?

Thanks

Dani AI

Generated

Calling it an "anonymous constructor" is a common mix-up. The short form you saw that omits a named type is an anonymous type; the brace-based form that follows a named type is an object initializer. called this out correctly, and was right to point at object initializers but used the wrong name for the anonymous-type syntax.

Concise differences to keep in mind: anonymous types are compiler-generated, sealed classes created when you write a type-less new { ... } expression. Their properties are initialized at creation (they are read-only from user code), the compiler generates Equals/GetHashCode/ToString, and you normally hold them with var because there is no public type name. Object initializers apply to a named type: the chosen constructor runs first, then the listed fields/properties are assigned. Object initializers require accessible setters (or init-only members in newer C#) and can be used together with constructor arguments.

Practical implications and pitfalls: anonymous types are excellent for short-lived projections (LINQ, shaping local results) but are not suited for public APIs or cross-layer returns — prefer a named DTO, a tuple, or a record for those scenarios. Object initializers are handy to avoid multiple assignment lines but do not bypass constructors or visibility rules. Also note anonymous-type equality is value-based for the generated properties, which can be useful for comparisons in local scopes.

For authoritative guidance and examples see Microsoft’s documentation on anonymous types and on object and collection initializers.

Recommended Answers

All 2 Replies

Hi,

I came across this code snippet:

new { genre = genreName }

I am not sure what is the name of this thing and how this thing works?

Thanks

Hmmm, not sure i f this is enough to go on, although I think the code snippet should be

Genre myGenre = new Genre{ genre = genreName };

This would instantiate an object of Genre and set the property genre to genreName. It seems to be called anonymous constructor alot but is also known as Object Initializer. It allows access to the properties without an overloaded constructor.

In the Genre example

public class Genre
    {
        string genre { get; set; }
        string description { get; set; }
        public Genre()
        {
            //empty constructor, never do this :P
        }
    }

Instead of doing this

Genre myGenre = new Genre();
myGenre.genre = SomeGenre;
mygenre.description = SomeDescrioption;

I can do this

Genre myGenre = new Genre {genre = SomeGenre, description = SomeDescription};
Genre myOtherGenre = new Genre {genre = SomeOtherGenre};

anonymous constructor AKA Object Initializer gives a nice example.

commented: Very likely exactly what's going on. Good example :) +1

>what is the purpose of anonymous constructor?

That is called Anonymous Types not an anonymous constructor. Read this thread.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.