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

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:


_______________________________________________________________________

Add to My Yahoo!


FREE Subscription

Subscribe to our mailing list and receive new articles
through email. Keep yourself updated with latest
developments in the industry.

Name:
Email:

Note : We never rent, trade, or sell my email lists to
anyone. We assure that your privacy is respected
and protected.

Visit .NET Programming Tutorial Homepage

______________________________________________________

Recommended Resource

| 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? |


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

Copyright - © 2004 - 2017 - All Rights Reserved.