Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Assertions in C#
Assertions in C# are debugging tools used to check assumptions in your code during development. They have two main arguments − a boolean expression that should evaluate to true, and an optional message to display if the assertion fails.
Assertions are particularly useful in large and complex programs to quickly identify logic errors that may arise when code is modified. They help catch bugs early in the development process.
Syntax
Following is the syntax for using Debug.Assert() −
Debug.Assert(condition); Debug.Assert(condition, "Error message"); Debug.Assert(condition, "Error message", "Detailed message");
Key Rules
-
Assertions are only active in Debug builds − they are removed in Release builds.
-
Never put side effects (method calls that change state) directly in Assert statements.
-
The code should work correctly even if all Assert statements are removed.
-
Use assertions to verify assumptions, not to handle expected errors.
Using Debug.Assert for Basic Validation
Example
using System;
using System.Diagnostics;
class Program {
public static void Main() {
int[] numbers = {5, 10, 15, 20};
int index = 2;
Debug.Assert(index >= 0 && index < numbers.Length,
"Index is out of bounds");
Console.WriteLine("Element at index " + index + ": " + numbers[index]);
// This assertion will pass
Debug.Assert(numbers[index] > 0, "Value should be positive");
Console.WriteLine("All assertions passed successfully");
}
}
The output of the above code is −
Element at index 2: 15 All assertions passed successfully
Avoiding Side Effects in Assertions
When using methods that modify state, store the result in a temporary variable to avoid issues when assertions are removed in Release builds −
Example
using System;
using System.Diagnostics;
class Counter {
private static int count = 5;
public static int DecrementCounter() {
count--;
return count;
}
public static void Main() {
Console.WriteLine("Initial count: " + count);
// WRONG: Side effect in assertion
// Debug.Assert(DecrementCounter() >= 0);
// CORRECT: Use temporary variable
int temp = DecrementCounter();
Debug.Assert(temp >= 0, "Counter should not be negative");
Console.WriteLine("Count after decrement: " + temp);
Console.WriteLine("Assertion completed successfully");
}
}
The output of the above code is −
Initial count: 5 Count after decrement: 4 Assertion completed successfully
Assertion Failure Example
Example
using System;
using System.Diagnostics;
class Division {
public static double Divide(int numerator, int denominator) {
Debug.Assert(denominator != 0, "Division by zero detected",
"Denominator value: " + denominator);
return (double)numerator / denominator;
}
public static void Main() {
Console.WriteLine("10 / 2 = " + Divide(10, 2));
Console.WriteLine("15 / 3 = " + Divide(15, 3));
// This would trigger assertion in Debug mode
// Console.WriteLine("8 / 0 = " + Divide(8, 0));
Console.WriteLine("Division operations completed");
}
}
The output of the above code is −
10 / 2 = 5 15 / 3 = 5 Division operations completed
Comparison
| Debug.Assert | Exception Handling |
|---|---|
| Only active in Debug builds | Active in both Debug and Release builds |
| Used for debugging and development | Used for handling expected runtime errors |
| Checks assumptions and invariants | Handles recoverable error conditions |
| Should not affect program logic | Part of normal program flow control |
Conclusion
Assertions in C# using Debug.Assert() are valuable debugging tools that help verify assumptions during development. Remember to avoid side effects in assertion statements and use temporary variables when calling methods that modify state to ensure your code works correctly in both Debug and Release builds.
