Init only collection property contructor from IEnumerable
I was wondering if there was a way to assign an IEnumerable to a init only collection property. For example I can do
But I was wondering if I could somehow do
I'm currently just looping through the enumerable and doing
psi.ArgumentList.Add(a);
as a fallback but just wanted to check if there was another way13 Replies
Best you can do in this case would be
IEnumerable<string> = new List<string>() { "foo", "bar" }
My use case is specifically where the enumerable is through an arg and wanting to set that as the ProcessStartInfo ArgumentList property. From what I can see online it doesn’t seem possible but I thought I would ask in case I’m missing something simple
ProcessStartInfo.ArgumentList
is a System.Collections.ObjectModel.Collection<string>
, a class with an IList<T>
constructor so you can simple convert the enumerable to a list and pass it it's readonly so you can't set it
or at least the only way I can set it is with
= { "foo", "bar" },
Learning something new :dviperHmm: Seems this syntax just calls Add under the hood anyway so looping and adding each item wouldn't be a huge deal in comparison
So would be equivalent to but the collection initializer syntax is only possible during initialization
Object and Collection Initializers - C# Programming Guide - C#
Object initializers in C# assign values to accessible fields or properties of an object at creation after invoking a constructor.
Makes sense, I'm just sticking with the foreach loop with Add 🙂
since C# 12 u can use collection expressions
Collection expressions (Collection literals) - C#
Collection expressions are expressions that convert to many different collection types. They enable you to write literal values for collection elements, or import other collection elements into a new collection.
eg
IEnumerable<string> arguments = ["a", "bc", "def"];
or IEnumerable<string> arguments = [..someStringCollectionA, "b", ..someOtherStringCollectionC];
(..
is called spread operator here)The question is about initializing a get-only collection from an IEnumerable. The collection initializer is a workaround that allow adding items to a mutable collection when the reference itself is readonly
https://sharplab.io/#v2:EYLgZgpghgLgrgJwgZwLQGED2AbbEDGMAlpgHYAyRMECU2yAPgAIBMAjALABQTADAARM2AFgDc3PoLYA6ACJEoAc1KZkxfMnFcJAZkEt+6fgG9u/c4L1Nh/ALIAKIToA8Q3gD5+UBIrgBbCFIYZABKEzMLSIAFBEx8FGQAZRhvGABJUjBMfgAHZCJ+AF5+UggAd3sQiMjzUy4ahv4AQR9/QJhKNSL+AG1+aWlvXwCg5H4AXWqagF8tSOnuaaA===
Just to clarify as well, seems like the option won't work. I've fine with the manual looping, just wanted to check if I was missing something
SharpLab
C#/VB/F# compiler playground.
Might want to keep an eye on future c# releases. It seems like a small oversight with all the syntax sugar that exists especially regarding collections in recent versions.
i totally agree. optimizations for the collection literals will come (iirc some are already merged for .net 9), but generally speaking they provide a way that u would only have to recompile without touching the code at all to get new optimizations in, instead of using some self written code.
(tho there are some uncommon use cases where u cant wait for better performance, but those ppl know what they are doing)
sadly the whole thing doesnt apply here because collection literals require that u can assign a new instance