If we want a single IEnumerable<T>
representing the concatenation of of two IEnumerable<T>
s we can use the LINQ Concat()
method.
For example:
int[] a = new int[] { 1, 2, 3 };
int[] b = new int[] { 4, 5 };
foreach (int i in a.Concat(b))
{
Console.Write(i);
}
of course outputs 12345
.
My question is, why is there no overload of Concat()
just accepting a single element of type T
such that:
int[] a = new int[] { 1, 2, 3 };
foreach (int i in a.Concat(4))
{
Console.Write(i);
}
would compile and produce the output: 1234
?
Googling around the issue throws up a couple of SO questions where the accepted answer suggests that the best approach when looking to achieve this is to simply do a.Concat(new int[] {4})
. Which is fine(ish) but a little 'unclean' in my opinion because:
- Maybe there is a performance hit from declaring a new array (albeit this is presumably going to be negligible pretty much evey time)
- It just doesn't look as neat, easy to read and natural as
a.Concat(4)
Anyone know why such an overload doesn't exist?
Also, assuming my Googling hasn't let me down - there is no such similar LINQ extension method taking a single element of type T
.
(I understand it is trivially easy to roll one's own extension method to produce this effect - but doesn't that just make the omission even more odd? I suspect there will be a reason for it's omission but can't imagine what it could be?)
UPDATE:
Acknowledging the couple of votes to close this as opinion based - I should clarify that I am NOT seeking peoples opinions on whether this would be a good addition to LINQ.
More I am seeking to understand the FACTUAL reasons why it is not ALREADY part of LINQ.