params TCollection<T> #6491
Replies: 11 comments 1 reply
-
How would you create a params of an interface. Specifically IEnumerable? |
Beta Was this translation helpful? Give feedback.
-
UPDATED 2022-10-07 ORIGINAL RESPONSE So, depending on your situation, you might want to do any of the following:
In all three of those situations, |
Beta Was this translation helpful? Give feedback.
-
@TonyValenti That would be the advantage of allowing params IEnumerable instead. I do like your proposal, and I think it is probably better than allowing IEnumerable params, I'm just raising a potential limitation with it. |
Beta Was this translation helpful? Give feedback.
-
A method shouldn't dictate the concrete type of parameters that it is using though beyond what is necessary to implement the algorithm that the method is encapsulating. And that encapsulation never includes the construction of the parameter because that happens outside of the method call. |
Beta Was this translation helpful? Give feedback.
-
Meh That's more of a heuristic than a hard and fast rule. Over here I think there is very little harm that would cause, and the benefit outways any code purity concerns I have. |
Beta Was this translation helpful? Give feedback.
-
@bbarry, while I do agree with you, @YairHalberstadt If I have:
I should still be able to call it using any of the following methods:
I do suppose that it could be agreed that something like:
Would result in a call-site transformation to List if the 'params' items are listed.
|
Beta Was this translation helpful? Give feedback.
-
Also not that However I don't think that's a genuine issue, as it's already special cased for collection initialisers. |
Beta Was this translation helpful? Give feedback.
-
Also linking #179 which has a link to a proposal for a builder pattern to construct |
Beta Was this translation helpful? Give feedback.
-
It gets a bit verbose, but the
That would allow you to pick the most efficient enumerable type for your particular algorithm rather than always using the same default. Users could even consider that as a useful hint on which type works best with your method in case they are constructing their own collection. |
Beta Was this translation helpful? Give feedback.
-
What collection would be more efficient than an array? Per #179 that's what the client would generate. Your suggestion would make the feature vastly more complicated and I don't see any benefit to it. |
Beta Was this translation helpful? Give feedback.
-
Converting to a discussion. This should be considered as part of #179. |
Beta Was this translation helpful? Give feedback.
-
Hi All,
I've seen different requests for params IEnumerable and such and I had a though I wanted to share.
Right now params requires a single dimension array. What if Params would allow any type with a default constructor and collection initializer method/extension methods?
Could be transformed by the compiler into:
This would basically give people the ability to determine the type of params they want. LinkedList, List, HashSet, etc.
UPDATED 2022-10-07
In the event that
params IEnumerable<T>
is specified, the compiler will be free to select the type used to construct the enumerable.#1366
#619
#1757
#178
Beta Was this translation helpful? Give feedback.
All reactions