skip to content
Khalid Abuhakmeh logo Khalid Abuhakmeh

Discards

C# 7

Code card: Discards. Program output: How high? 10 high! / False

_ ignores tuple elements and out values you don't need and declares no variable. Skip values you don't need by assigning them to the _ discard. It works with tuple deconstruction, out parameters and patterns. It is available since C# 7. The key line is var (_, y) = point in discard.cs. Run it and the output reads How high? 10 high!, False.

Did this tip save you some time? Consider buying me a coffee to fuel the next one.

Buy me a coffee
Railway Referral

Need somewhere to deploy?

My link gets you $20 in credits.

Claim $20 credit

I may earn a commission if you become a customer.

discard.cs
var point = (x: 0, y: 10);
var (_, y) = point;
Console.WriteLine($"How high? {y} high!");
var ok = int.TryParse("nope", out _);
Console.WriteLine(ok);
Output
$ dotnet run discard.cs
How high? 10 high!
False