ADVERTISEMENT
Google AdSense Slot (headerLeaderboard)
Set your Publisher ID in src/config/ads.ts to display live ads.
C# Tutorial• 7 min read
Methods, Parameters & Pattern Matching
Named arguments, ref/out parameters, switch expressions, and property patterns
Modern C# excels at pattern matching, allowing concise data inspection using switch expressions and type patterns.
Switch Expressions & Out Parameters
Switch Expressions: Evaluate to values directly: var result = score switch { >= 90 => "A", >= 80 => "B", _ => "F" };.
out parameters: Allows methods to return multiple values (e.g. int.TryParse(str, out int val)).
C# Pattern Matching in Action
public record Customer(string Name, int OrdersCount, bool IsPremium);
public class DiscountCalculator {
public static decimal GetDiscount(Customer c) => c switch {
{ IsPremium: true, OrdersCount: > 10 } => 0.20m, // 20%
{ IsPremium: true } => 0.10m, // 10%
{ OrdersCount: > 5 } => 0.05m, // 5%
_ => 0.00m
};
}ADVERTISEMENT
Google AdSense Slot (inContentBanner)
Set your Publisher ID in src/config/ads.ts to display live ads.
Test Yourself With Exercise
In a C# switch expression, what symbol serves as the catch-all wildcard (equivalent to `default` in classic switch)?