Home >Backend Development >C#.Net Tutorial >Assert in C#
The following articles provide an outline on Assert in C#. The assert method is one of the most effective methods to detect logic errors at runtime and making it easy to correct the error at the production level. An assert method generally takes 2 arguments: one is a boolean expression and another is a message to be displayed. While the assert method takes two arguments, there must not be a single function inside the assert method and in no shall it has any impact on the out of the program, in any way. The assert method is convenient to implement in large programs, where it allows programmers to easily detect and clear errors.
Syntax:
Debug.Assert();
Above debug.assert method is part of System.Diagnostics class and provides a way to speedily implement the function. Debug class varies from Trace class where it is only included in Debug Build, while Trace class is included in Debug and Release Build. It is advisable to not to use any specific function call inside this assert method in any part of the program. It is important to understand that the inside function shall not have any impact on output.
While defining an assert method we have to pass two arguments, one is a boolean value and another is to be a message that must be displayed. Assert method works with having either True or False:
When a program encounters the assert method, it will check for the condition. The program will be interrupted and will inform you the condition is not met. If the condition is false, the second argument which is a message will be displayed. The program will proceed in case of the condition is true.
Basically, when we have inserted an assert at any point in the program, if the condition is found to be false, it will interrupt the normal execution of the program and display a dialog box with details.
Given below are the examples are mentioned:
An assert method with simple integer with any specific function.
Code:
using System; using System.Diagnostics; namespace assert_sim { static class Program { public static void Main() { int val = 2; Debug.Assert(val != 2, " Value should not be 2."); } } }
Code Interpretation:
Output:
When the value was 2, as explained earlier, dialog was displayed with message, “Value must never be 2” along with details of the error. Message will display the line number where it caught the assert method.
Addition of two numbers and will pass on to assert method for condition.
Code:
using System; using System.Diagnostics; namespace assert_sim { static class Program { public static void Main() { int x = 2; int y = 2; int q = x + y; Console.WriteLine("This is C# Assert Example."); Debug.Assert(q != 4, "Addition should not be 4."); Console.WriteLine("\n This is after assert method."); Console.ReadLine(); } } }
Code Interpretation:
Output:
And, clicking on Ignore button, the dialog box will disappear and the last line will be printed.
With every specific method or function in programming language, we have multiple advantages, just like that following are the advantages of using assert method in c#:
Assert method is simply used to identify errors in runtime. Assert Method takes two arguments, first is a boolean expression, where the condition is checked, and second is message to display based on the result of the condition. We demonstrated two examples to understand the working of the assert method. One of the best applications for Assert is to implement it with quite a large program, as it makes the process of locating and quickly removing the errors.
The above is the detailed content of Assert in C#. For more information, please follow other related articles on the PHP Chinese website!