What is the purpose of assert() in C#?

While developing an application, there are high probabilities for logical errors to occur. During development phase, you can trap those errors using assert method.



You can include a logical condition in the assert method. If the condition evaluation returns false then the assert fails and appropriate call stack is displayed to the User. There are many overloaded versions of assert method. Here is an example to demonstrate the usage of this method:

namespace Application1 {
public class sampleClass {
public static void Main() {
Console.WriteLine("Enter the number:");
int sampleNum = Int32.Parse(Console.ReadLine());
Debug.Assert(sampleNum > 0);
float var1 = 100 / sampleNum;
Console.WriteLine("Result = {0}", var1);
}
}
}

Execute this program and enter the number as 0. Now the condition sampleNum>0 will fail. Hence you will get the following assertion failure message:


| What is the need for Bridge Pattern in C#? | What is the need for Builder Pattern in C#? | What is the need for Chain of Responsibility Pattern in C#? | What is the need for Command Pattern in C#? | What is the need for Composite Pattern in C#? | What is the need for Decorator Pattern in C#? | What is the need for Flyweight Pattern in C#? | What is the need for Interpreter Pattern in C#? | What is the need for Iterator Pattern in C#? | What is the need for Mediator Pattern in C#? | What is the need for Memento Pattern in C#? | What is the need for Prototype Pattern in C#? | What is the need for State Pattern in C#? | What is the need for Strategy Pattern in C#? | What is the need for Template Method Pattern in C#? | What is the need for unsafe code in C#? | What is the purpose of assert() in C#? | What is the purpose of AutoResetEvent in .NET? | What is the purpose of Console.ReadLine() in C#? | What is the purpose of machine.config file in .NET? |


“Amazon and the Amazon logo are trademarks of Amazon.com, Inc. or its affiliates.”

| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2024 - All Rights Reserved.