assert() method in c#
what is use of assert() method in c# give any example
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Nilesh JadavPosted Oct 14, 2015, 6:26 AM
An assertion is code that’s used during development—usually a routine or macro—that allows a program to check itself as it runs. When an assertion is true, that means everything is operating as expected. When it’s false, that means it has detected an unexpected error in the code.
For example, if the system assumes that a customer-information file will never have more than 50,000 records, the program might contain an assertion that the number of records is less than or equal to 50,000. As long as the number of records is less than or equal to 50,000, the assertion will be silent. If it encounters more than 50,000 records, however, it will loudly “assert” that there is an error in the program.
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.Thank you !!
--------------------------------------------------
If you satisfy with the answer Please Accept it.
Profile Visit: https://usingaspdotnet.wordpress.com
Rajeesh MenothPosted Oct 14, 2015, 6:09 AM