3

Example:

var timer1 = new Timer(_ => Console.WriteLine("Hello World"), null, 0, 2000);
Markus Safar
  • 6,324
  • 5
  • 28
  • 44
  • https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/functional/discards – Hans Kilian Jan 06 '22 at 08:59
  • 2
    Not a discard, just a variable name that stores the *state* argument passed to the TimerCallback callback. Any name will do, but _ is often used to indicate that you meant not to use it. – Hans Passant Jan 06 '22 at 09:11
  • See the "Note" [here](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/lambda-expressions#input-parameters-of-a-lambda-expression): "For backwards compatibility, if only a single input parameter is named `_`, then, within a lambda expression, `_` is treated as the name of that parameter". So this isn't a discard technically -- it's a variable called `_` – canton7 Jan 06 '22 at 09:11

1 Answers1

1

Begining with C# 7.0, C# started to support discards. _ is consider discards which are basically a placeholder variables that are intentionally unused in application code. Starting with C# 9.0, you can also use discards to specify unused input parameters of a lambda expression (like in your example).

discards are basically just a way to intentionally ignore irrelevant local variables for the purposes of the code that is being produced.So, for example when you call a function that returns a value but you are only interested in the underlying operations it does, you don't have to assign its output to a local variable defined in the caller method.

Because there can only be a single discard variable, that variable may not even be allocated storage so Discards can actually reduce memory allocations while also making the intent of your code clearer, enhance its readability and maintainability.

When _ is a valid discard, trying to access its value or modify it in an assignment operation will generate a compiler error ("The name '_' doesn't exist in the current context"). This error is being generated because _ isn't assigned a value, and may not be assigned a storage location.

More about Discards can be read in this link

Ran Turner
  • 14,906
  • 5
  • 47
  • 53
  • Thank you for your answer but please mark duplicate questions as duplicate (+1 anyway ;-) ) – Markus Safar Jan 06 '22 at 09:06
  • 1
    Thanks for this comment @canton7 - I will modify my answer – Ran Turner Jan 06 '22 at 09:13
  • If you feel this answer is better than the ones posted against [Discard feature significance in C# 7.0?](https://stackoverflow.com/questions/48970312/discard-feature-significance-in-c-sharp-7-0), please add it there. – Johnathan Barclay Jan 06 '22 at 09:23
  • @Johnathan but.. what about the unicorn points? – Caius Jard Jan 06 '22 at 09:27
  • The question was closed after i posted the answer, I will put more attention from now on – Ran Turner Jan 06 '22 at 09:32
  • OK, so now people who find [Discard feature significance in C# 7.0?](https://stackoverflow.com/questions/48970312/discard-feature-significance-in-c-sharp-7-0) will miss out on this answer. Answers to the same question should be in the same place. @RanTurner, there's still time to post it there :) – Johnathan Barclay Jan 06 '22 at 09:34