Compile cs file to DLL

   
 
// Math.cs
   public class Math
   {
      ///<summary>
      ///   The Add method allows us to add two integers
      ///</summary>
      ///<returns>Result of the addition (int)</returns>
      ///<param name="x">First number to add</param>
      ///<param name="y">Second number to add</param>
      public int Add(int x, int y)
      {
         return x + y;
      }
   }
//csc /t:library /doc:Math.xml Math.cs

    


Using Switches to Control Debug and Trace:User-Defined Switch

   

// compile with: csc /r:system.dll file_1.cs

using System;
using System.Diagnostics;

enum SpecialSwitchLevel
{
    Mute = 0,
    Terse = 1,
    Verbose = 2,
    Chatty = 3
}

class SpecialSwitch: Switch
{
    public SpecialSwitch(string displayName, string description) :
    base(displayName, description)
    {
    }
    
    public SpecialSwitchLevel Level
    {
        get
        {
            return((SpecialSwitchLevel) base.SwitchSetting);
        }
        set
        {
            base.SwitchSetting = (int) value;
        }    
    }
    public bool Mute
    {
        get
        {
            return(base.SwitchSetting == 0);
        }
    }
    
    public bool Terse
    {
        get
        {
            return(base.SwitchSetting >= (int) (SpecialSwitchLevel.Terse));
        }
    }
    public bool Verbose
    {
        get
        {
            return(base.SwitchSetting >= (int) SpecialSwitchLevel.Verbose);
        }
    }
    public bool Chatty
    {
        get
        {
            return(base.SwitchSetting >=(int) SpecialSwitchLevel.Chatty);
        }
    }
    
    protected new int SwitchSetting
    {
        get
        {
            return((int) base.SwitchSetting);
        }
        set
        {
            if (value < 0)
            value = 0;
            if (value > 4)
            value = 4;
            
            base.SwitchSetting = value;
        }
    }
}

class MyClass
{
    public MyClass(int i)
    {
        this.i = i;
    }
    
    [Conditional("DEBUG")]
    public void VerifyState()
    {
        Console.WriteLine("VerifyState");
        Debug.WriteLineIf(debugOutput.Terse, "VerifyState Start");
        
        Debug.WriteLineIf(debugOutput.Chatty, 
        "Starting field verification");
        
        if (debugOutput.Verbose)
        Debug.WriteLine("VerifyState End");
    }
    
    static SpecialSwitch    debugOutput = 
    new SpecialSwitch("MyClassDebugOutput", "application");
    int i = 0;
}

public class TraceUserDefinedSwitch
{
    public static void Main()
    {
        //TraceSwitch ts = new TraceSwitch("MyClassDebugOutput", "application");
        //Console.WriteLine("TraceSwitch: {0}", ts.Level);
        
        Debug.Listeners.Clear();
        Debug.Listeners.Add(new TextWriterTraceListener(Console.Out));
        MyClass c = new MyClass(1);
        
        c.VerifyState();
    }
}

           
          


Using Switches to Control Debug and Trace:TraceSwitch

   

using System;
using System.Diagnostics;

class MyClass
{
    public MyClass(int i)
    {
        this.i = i;
    }
    
    [Conditional("DEBUG")]
    public void VerifyState()
    {
        Debug.WriteLineIf(debugOutput.TraceInfo, "VerifyState Start");
        
        Debug.WriteLineIf(debugOutput.TraceVerbose, 
        "Starting field verification");
        
        if (debugOutput.TraceInfo)
        Debug.WriteLine("VerifyState End");
    }
    
    static TraceSwitch    debugOutput = 
    new TraceSwitch("MyClassDebugOutput", "Control debug output");
    int i = 0;
}

public class TraceTraceSwitch
{
    public static void Main()
    {
        Debug.Listeners.Clear();
        Debug.Listeners.Add(new TextWriterTraceListener(Console.Out));
        MyClass c = new MyClass(1);
        
        c.VerifyState();
    }
}

           
          


Using Switches to Control Debug and Trace:BooleanSwitch

   

// compile with: csc /D:DEBUG /r:system.dll boolean.cs

using System;
using System.Diagnostics;

class MyClass
{
    public MyClass(int i)
    {
        this.i = i;
    }
    
    [Conditional("DEBUG")]
    public void VerifyState()
    {
        Debug.WriteLineIf(debugOutput.Enabled, "VerifyState Start");
        
        if (debugOutput.Enabled)
        Debug.WriteLine("VerifyState End");
    }
    
    BooleanSwitch    debugOutput = 
    new BooleanSwitch("MyClassDebugOutput", "Control debug output");
    int i = 0;
}

public class UsingSwitchestoControlDebugandTraceBooleanSwitch
{
    public static void Main()
    {
        Debug.Listeners.Clear();
        Debug.Listeners.Add(new TextWriterTraceListener(Console.Out));
        MyClass c = new MyClass(1);
        
        c.VerifyState();
    }
}

           
          


Debug and Trace Output

   

using System;
using System.Diagnostics;

class MyClass
{
    public MyClass(int i)
    {
        this.i = i;
    }
    
    [Conditional("DEBUG")]
    public void VerifyState()
    {
        Debug.WriteLineIf(debugOutput, "In VerifyState");
        Debug.Assert(i == 0, "Bad State");
    }
    
    static public bool DebugOutput
    {
        get
        {
            return(debugOutput);
        }
        set
        {
            debugOutput = value;
        }
    }
    
    int i = 0;
    static bool debugOutput = false;
}

public class DebugandTraceOutput
{
    public static void Main()
    {
        Debug.Listeners.Clear();
        Debug.Listeners.Add(new TextWriterTraceListener(Console.Out));
        MyClass c = new MyClass(1);
        
        c.VerifyState();
        MyClass.DebugOutput = true;
        c.VerifyState();
    }
}