Csharp/C Sharp by API/System.Diagnostics/Debug — различия между версиями

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

Версия 15:31, 26 мая 2010

Debug.Assert

  
using System;
using System.Diagnostics;
using System.Collections;
using System.Data;
using System.Text;
public class MainClass{
   public static void Main(){
     Debug.Assert(1 == 0,"Error!","Error: 1 == 0");
   }
}


Debug.Fail

  
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");
   }
}


Debug.Listeners

  
// 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();
    }
}


Debug.WriteLine

  
#define DEBUG
using System;
using System.Globalization;
using System.Diagnostics;
class MainClass
{
  public static void Main() 
  {
    TextWriterTraceListener tl = new TextWriterTraceListener(Console.Out);
    Debug.Listeners.Add(tl);
    Debug.WriteLine("Starting Main()");
    Debug.Assert(1 == 2, "1==2");
    Debug.WriteLine("Exiting Main()");
  }
}