C
C#10mo ago
__dil__

✅ Introduce variable in `while` condition

Is it possible to achieve something like this? (pseudo-C#)
while (Computation() val when val != otherVal)
// You can use `val` here...
while (Computation() val when val != otherVal)
// You can use `val` here...
The idea is that the loop should go until the result of some computation does not match some predicate, and to also keep the result of the computation around in the loop. If not, how would you achieve a similar pattern?
7 Replies
Thinker
Thinker10mo ago
while (Computation() is var val && val != otherVal)
while (Computation() is var val && val != otherVal)
bit of a strange syntax ik is var val is a pattern which just declares a variable returns true
__dil__
__dil__10mo ago
Oh that's great, thanks 😄
reflectronic
reflectronic10mo ago
there is also the classic
T val;
while ((val = Computation()) != otherVal)
T val;
while ((val = Computation()) != otherVal)
Thinker
Thinker10mo ago
yikes
reflectronic
reflectronic10mo ago
it, admittedly, is not a one-liner
__dil__
__dil__10mo ago
I really don't like variable declarations without initialization, but yeah that's a good pattern to know too
Accord
Accord10mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server
More Posts
✅ How do I specify custom base class for user control?I declared a custom base class derived from `UserControl`. Now in a second assembly, I need to deri✅ My solution contains absolute paths to projects inside the solution folderI have a solution where all the projects are inside the solution's folder. But VS Community 2022 in❔ Editing the DOM with JS and BlazorHey, I'm building a UI with Blazor Server and I have a question about JS interacting with the DOM in✅ my code to calculate an average seems to be wrong```using System; using System.Reflection.Metadata; namespace startover { internal class Progra✅ Message me if you are good at C# I need some help I am beginner I know bit Javascript and a Bit C#Message me if you are good at C# I need some help I am beginner I know bit Javascript and a Bit C#❔ Show error text message under update text box through server validation without refreshingSo basically I want to show an error text message if the user updates and submits an invalid value, ❔ Roslyn analyzer rules extractionPls guide me with a solution for extracting a set of rules from any RoslynAnalyzer.dll that it conta✅ filling byte array with a random intbest approach? ```cs BinaryPrimitives.WriteInt32BigEndian(bytes, Random.Shared.Next(5000000)); //vs ✅ How to pattern-match a `Rune`?is this the best I can do? ```cs myRune switch { _ when myRune == (Rune)'A' => 3, _ when myR❔ EF Core LINQ - Many to Many Search with && and ||Hi there, I have a 3 table structure for file storage that I would like to search, consisting of th