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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
Rajeesh MenothRank #118 · 18.4K reputationPosted Oct 14, 2015, 2:09 AM EDT
Nilesh JadavRank #109 · 20.3K reputationPosted Oct 14, 2015, 2:26 AM EDT
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