csharp / beginner
Snippet
Diagnostic Logic Checks
Debug assertions are used to check for logic errors during development; they are automatically removed from the final production build.
snippet.cs
csharp
1
2
3
4
5
6
7
8
using System.Diagnostics;public void SetUserAge(int age){// Validates assumptions during developmentDebug.Assert(age >= 0, "Age cannot be negative!");System.Console.WriteLine($"Age set to {age}");}
Breakdown
1
Debug.Assert
Checks a condition and shows a message if the condition is false.
2
age >= 0
The logical condition that must be true for the code to be correct.