.NET Array Dictionary List String 2D Async DataTable Dates DateTime Enum File For Foreach Format IEnumerable If IndexOfLambda Parse Path Process Property Regex Replace Row Sort Split Static StringBuilder Substring Switch Tuple Window
Assert sends a strong message to the developer. An assertion interrupts normal operation of the program but does not terminate the application. The Debug.Assert method in the System.Diagnostics class provides a way to implement this.Note:Debug calls are compiled out when in Release mode. Exceptions are always kept in the code.
ExceptionC# program that uses Assert method using System; using System.Diagnostics; static class Program { static void Main() { int value = -1; // A. // If value is ever -1, then a dialog will be shown. Debug.Assert(value != -1, "Value must never be -1."); // B. // If you want to only write a line, use WriteLineIf. Debug.WriteLineIf(value == -1, "Value is -1."); } } Result A. The dialog is displayed. B. Message is written to the Output: Value is -1.
Then:The Debug.Assert method will be executed. It internally checks the value of this expression evaluation.
Overview:The Assert method is most disruptive, and other methods such as Debug.WriteLine are less of a burden to encounter.
Summary. Here we looked at an example of using the Debug.Assert method and an expression-based parameter to display an error condition to a developer of the program. We saw the error message displayed by Assert by a real C# program.
转载地址:http://www.dotnetperls.com/assert