Add support for Discards inside a foreach loop (underscore syntax, placeholder variables) #7888
Replies: 2 comments 12 replies
-
Do you have any real-world use cases for this? My gut feeling is that scenarios where one needs to repeat something 100 times without reference to a counter seem likely vanishingly rare. |
Beta Was this translation helpful? Give feedback.
-
If this was a true discard, where no reference was kept to the item for the current iteration, then it would be eligible for garbage collection. Now, if you're iterating But if you're iterating some kind of factory that implements With this: foreach (var case in factory.GenerateCases())
{
// ....
} each generated case lives in a local variable and definitely won't be garbage collected prior to the end of the iteration. Same applies to this: foreach (var _in factory.GenerateCases())
{
// ....
} where Change Having spent a migraine inducing few days tracking down a problem precisely like this, where a dropped reference resulted in early GC when the server was under load (the bug was not easily reproduced on my dev machine), I can assure you this would be no fun at all. The obvious fix would be for the compiler to still emit a variable behind the scenes, at which point the generated code is precisely the same and all we've gained is a the ability to delete a few characters from maybe 1% of foreach loops. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Add support for Discards inside a
foreach
loop (underscore syntax, placeholder variables).One of the next:
Beta Was this translation helpful? Give feedback.
All reactions