0 day streak
csharp / beginner
Snippet

Logic Verification Assertions

Debug.Assert is used to check for conditions that should always be true during development, helping catch logic bugs early.

snippet.cs
csharp
1
2
3
4
5
6
7
8
9
10
11
12
using System.Diagnostics;
public class Program {
public static void ProcessAge(int age) {
Debug.Assert(age >= 0, "Age cannot be negative!");
System.Console.WriteLine($"Age is: {age}");
}
public static void Main() {
ProcessAge(25);
}
}
Breakdown
1
Debug.Assert(age >= 0, ...)
Checks the condition; if false, it triggers a break in the debugger with the provided message.