Anonymous types and with keyword

Using the with keyword to support non-destructive mutations on anonymous types

Home DailyDrop

Daily Knowledge Drop

The with keyword can be used to create a new instance of an anonymous type where one of more properties have new values.


Anonymous types

First off, a brief explanation of anonymous types. Anonymous types are a way to encapsulate a set of read only properties into a single object without explicitly defining a type. The type name is generated internally by the compiler and the type of each property is inferred by the compiler.

Here we define an anonymous type (as you can see, no type is specified) with the instance name song, and a number of properties:

var song = new
{
    Name = "Everlong",
    Artist = "Foo Fighters",
    Released = 1997
};

Console.WriteLine(song.Name);
Console.WriteLine(song);

The song instance can be treated and operated on as a usual type instance, with each property accessible (as read-only):

Everlong
{ Name = Everlong, Artist = Foo Fighters, Released = 1997 }

Using with

As mentioned above, the properties on an anonymous class are read-only.

This is NOT permitted:

var song = new
{
    Name = "Everlong",
    Artist = "Foo Fighters",
    Released = 1997
};

// Compiler will not allow this!
song.Name = "Monkey Wrench";

This is where the with keyword becomes useful - with expressions allow for non-destructive mutation of an anonymous type!

Suppose we wanted to instantiate an anonymous type for each song on the album - most of the information (Artist and Released) would be the same across each instance. The with keyword can be leveraged to create a new instance of the anonymous type, based on an existing instance, but with (some) new values.

var song = new
{
    Name = "Everlong",
    Artist = "Foo Fighters",
    Released = 1997
};

var song2 = song with { Name = "Monkey Wrench" };
var song3 = song with { Name = "My Hero" };
var song4 = song with { Name = "Walking After You" };

The output:

    { Name = Everlong, Artist = Foo Fighters, Released = 1997 }
    { Name = Monkey Wrench, Artist = Foo Fighters, Released = 1997 }
    { Name = My Hero, Artist = Foo Fighters, Released = 1997 }
    { Name = Walking After You, Artist = Foo Fighters, Released = 1997 }

Notes

When dealing with anonymous types, which share the same data across instances, this technique of instantiating using with and an existing instance can save time as well as keep code cleaner.


References

Anonymous Types

Daily Drop 42: 31-03-2022

At the start of 2022 I set myself the goal of learning one new coding related piece of knowledge a day.
It could be anything - some.NET / C# functionality I wasn't aware of, a design practice, a cool new coding technique, or just something I find interesting. It could be something I knew at one point but had forgotten, or something completely new, which I may or may never actually use.

The Daily Drop is a record of these pieces of knowledge - writing about and summarizing them helps re-enforce the information for myself, as well as potentially helps others learn something new as well.
c# .net anonymous with