The _ character in C# - all its meanings
This is a good question, because _ in C# has several completely different uses depending on the context - and it is a frequent "do you really understand the difference" topic in interviews.
1. A naming convention - private fields
public class OrderViewModel : ViewModelBase
{
private readonly IOrderService _orderService; // ← prefiks _ dla private fields
private readonly ILogger _logger;
public OrderViewModel(IOrderService orderService)
{
_orderService = orderService; // distinguishes the field from the constructor parameter
}
}
This is only a stylistic convention (from the official .NET coding style, widely adopted), not any special language syntax. _orderService is an ordinary identifier name - the compiler treats _ like any other character allowed in a variable name. It is mainly useful for distinguishing a class field from a parameter/local variable without needing this.orderService.
2. Discard (_) - "I'm throwing this value away, I don't care about it"
This is a real language feature (since C# 7.0) - _ as a placeholder telling the compiler "I know something is here, but I consciously won't use it".
a) Discard in tuple deconstruction
(string name, int age, string _) = GetPersonData();
// I care about name and age, I consciously ignore the third element (e.g. email)
var (id, _) = (order.Id, order.InternalDebugCode); // I ignore the second element
b) Discard in out parameters
// I only care WHETHER it succeeded, not about the value itself
if (int.TryParse(input, out _))
{
Console.WriteLine("To jest poprawna liczba");
}
// In contrast - if I needed the value:
if (int.TryParse(input, out int parsedValue))
{
Console.WriteLine($"Sparsowano: {parsedValue}");
}
c) Discard in pattern matching / switch expressions
This is especially common in modern C# (8.0+):
public string DescribeOrder(Order order) => order switch
{
{ Total: > 1000 } => "Large order",
{ Items.Count: 0 } => "Empty order",
_ => "Ordinary order" // ← the "default case", literally "anything else"
};
Here _ means "any value that doesn't match the earlier patterns" - the equivalent of default: in a traditional switch.
d) Discard as ignoring the result of a call
_ = _orderService.LogAccessAsync(orderId);
// I consciously "fire and forget",
// explicitly telling the compiler that I DON'T care about the returned Task
This is useful for silencing the compiler warning (CS4014 - "call is not awaited") in situations where you really consciously don't want to wait for the result - without _ = you would get a warning suggesting you probably forgot await.
3. _ as a lambda parameter you don't use
A similar idea to a discard, but in the context of lambdas - a convention (not enforced by the compiler, but readable):
// Timer callback - I don't care about any of the passed arguments
timer.Elapsed += (_, _) => RefreshData();
// W RelayCommand - parametr "object parameter" jest ignorowany
SaveOrderCommand = new RelayCommand(_ => SaveOrder());
A technical curiosity: since C# 9.0 you can use multiple _ as separate parameters in the same lambda (as in the (_, _) => ... example above) and the compiler accepts it as two separate discards, even though normally you can't have two variables with the same name in one scope.
4. An underscore as a digit separator in numeric literals (C# 7.0+)
A completely different use - it improves the readability of large numbers:
int million = 1_000_000; // more readable than 1000000
long bigNumber = 1_000_000_000_000L;
double pi = 3.14_15_92;
int binaryFlags = 0b1010_0101_1100_0011; // przydatne przy liczbach binarnych
int hexColor = 0xFF_EC_A5;
The _ is purely cosmetic here - the compiler ignores it, 1_000_000 and 1000000 are exactly the same value. It doesn't matter where exactly you put the underscores (they don't have to be every 3 digits).
How to tell which meaning is meant in a given piece of code
| Context | Meaning |
|---|---|
private readonly IFoo _foo; |
A field naming convention |
out _ / (a, _) / _ = SomeCall() |
Discard - consciously ignoring a value |
_ => DoSomething() in a lambda |
An unused parameter (a form of discard) |
_ as the last branch of a switch |
The "everything else" pattern (default) |
1_000_000 |
A digit separator in a numeric literal |
Why this matters in practice - avoiding compiler warnings
A common scenario where a discard actually changes the quality of the code:
// Bad - you create a variable you will never use - the compiler/analyzer will flag it (CS0168/IDE0059)
Dictionary<string, Order>.TryGetValue(id, out var unusedOrder);
// Good - you explicitly say "I know this is ignored"
Dictionary<string, Order>.TryGetValue(id, out _);
This is, incidentally, a difference that some static analyzers (Roslyn analyzers, ReSharper) actually enforce - out _ is treated as cleaner intent-revealing code than out var ignoredVariable, because it clearly communicates "this value is intentionally unused", rather than "I forgot to use it".