Quantcast
Channel: VBForums
Viewing all articles
Browse latest Browse all 15168

[RESOLVED] Increment Operator Not Working in Enumerable.Repeat

$
0
0
I found that using the postfix increment operator in Enumerable.Repeat doesn't increment the underlying variable. See for example:
Code:

var parameterIndex = 0;
var slugs = new[] {"one", "two"};
var slugParameters = Enumerable.Repeat($"@{parameterIndex++}", slugs.Length);
Console.WriteLine($" Status IN ({string.Join(", ", slugParameters)})");

Fiddle: https://dotnetfiddle.net/EnYStx

The output from above is:
Quote:

Status IN (@0, @0)
When I would expect it to be:
Quote:

Status IN (@0, @1)
What causes this behavior and is there a workaround aside from manually looping over the array?

Update
Apparently using a select works as expected:
Code:

var slugParameters = slugs.Select(slug => $"@{parameterIndex++}");
But I'm still curious why it doesn't work with Enumerable.Repeat.

Update #2
I see why now. It is calling:
Code:

$"@{parameterIndex++}"
Once and then repeating that result n times.

Viewing all articles
Browse latest Browse all 15168

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>