# C# Collection Initializer

**URL:** <https://forums.fsharp.org/t/c-collection-initializer/2836>\
**Category:** General\
**Created:** [January 26, 2023, 7:06pm UTC](https://forums.fsharp.org/t/c-collection-initializer/2836 "2023-01-26T19:06:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![nodakai](https://yyz2.discourse-cdn.com/flex030/user_avatar/forums.fsharp.org/nodakai/32/1167_2.png) [@nodakai](https://forums.fsharp.org/u/nodakai)\
**Post date:** [January 26, 2023, 7:06pm UTC](https://forums.fsharp.org/t/c-collection-initializer/2836/1 "2023-01-26T19:06:59Z")

</div>

F# [can almost do the same](https://learn.microsoft.com/en-us/dotnet/fsharp/language-reference/members/constructors#assigning-values-to-properties-at-initialization) with C# [Object Initializer](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/object-and-collection-initializers), but how about Collection Initializer? I’m using `fold` to call `.Add()` today but wonder if there’s any better way.

---

<div class="post-metadata">

**Author:** ![nodakai](https://yyz2.discourse-cdn.com/flex030/user_avatar/forums.fsharp.org/nodakai/32/1167_2.png) [@nodakai](https://forums.fsharp.org/u/nodakai)\
**Post date:** [January 26, 2023, 7:08pm UTC](https://forums.fsharp.org/t/c-collection-initializer/2836/2 "2023-01-26T19:08:14Z")

</div>

(splitting my post into two parts to circumvent Discourse’s URL count limit)

Use case: [Class InputMap\<V\>](https://www.pulumi.com/docs/reference/pkg/dotnet/Pulumi/Pulumi.InputMap-1.html) from Pulumi

Well, for this particular case, I think the API should be improved (_e.g._ by taking `IDictionary<string, V>`) but that’s probably another story.

---

<div class="post-metadata">

**Author:** ![Prash](https://yyz2.discourse-cdn.com/flex030/user_avatar/forums.fsharp.org/prash/32/815_2.png) [@Prash](https://forums.fsharp.org/u/Prash)\
**Post date:** [February 20, 2023, 3:45pm UTC](https://forums.fsharp.org/t/c-collection-initializer/2836/3 "2023-02-20T15:45:56Z")

</div>

That isn’t possible in F#. I would just suggest that using a plain old `for` loop would be better than a fold. It’s simpler and more obvious that you’re performing a side-effect
