0 day streak
csharp / intermediate
Snippet

Manual Unit Verification Pattern

In pure C# without external frameworks, you can implement verification methods to validate code correctness using standard exceptions and comparisons.

snippet.cs
csharp
1
2
3
4
5
6
7
8
9
10
11
public static void AssertEqual(int expected, int actual, string testName)
{
if (expected != actual)
{
throw new Exception($"Test '{testName}' Failed: Expected {expected}, but got {actual}");
}
Console.WriteLine($"Test '{testName}' Passed.");
}
// Example logic test:
// AssertEqual(10, CalculateSum(5, 5), "AdditionTest");
Breakdown
1
if (expected != actual)
Core comparison logic to verify the result against expectation.
2
throw new Exception(...)
Signals a failure by interrupting the execution flow.
3
string testName
Provides context to identify which specific check failed.