Csharp/C Sharp/Development Class/Assert Debug

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Defensive Programming:Asserts

// compile with: csc /r:system.dll file_1.cs
using System;
using System.Diagnostics;
class MyClass
{
    public MyClass(int i)
    {
        this.i = i;
    }
    
    [Conditional("DEBUG")]
    public void VerifyState()
    {
        Debug.Assert(i == 0, "Bad State");
    }
    
    int i = 0;
}
public class DefensiveProgrammingAsserts
{
    public static void Main()
    {
        Debug.Listeners.Clear();
        Debug.Listeners.Add(new TextWriterTraceListener(Console.Out));
        MyClass c = new MyClass(1);
        
        c.VerifyState();
    }
}


Use Debug fail method

using System;
using System.Collections;
using System.Diagnostics;
class Class1 {
   static void Main(string[] args) {
     Debug.Fail("Fail Message", "An unexpected exception has been thrown.\n\n");
   }
}