0 day streak
csharp / beginner
Snippet

Manual Truth Assertion

Testing ensures code reliability. In plain C#, you can create simple assertion methods to verify that your logic behaves as expected.

snippet.cs
csharp
1
2
3
4
5
6
7
8
9
10
11
12
13
public void RunTest()
{
int result = 5 + 5;
Assert(result == 10, "Sum should be 10");
}
private void Assert(bool condition, string message)
{
if (!condition)
{
throw new Exception($"Test Failed: {message}");
}
}
Breakdown
1
Assert(result == 10, ...)
Checks if the logic produced the correct value.
2
throw new Exception(...)
Signals a failure if the condition is not met.