C# FAQ: How do I use trace and assert?

Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio


Jump to: navigation, search
CSharp-Online.NET:FAQs
edit

How do I use trace and assert?

Add a conditional attribute to the method as indicated below:

using System.Diagnostics.ConditionalAttribute;
 
class Trace
{
   [conditional("TRACE")]
   public void Trace (string str)
   {
      Console.WriteLine (str);
   }
}
 
class Debug
{
   public static void Main()
   {
      Debug.Trace ("Main reached");
   }
}

In the preceding example, the Debug.Trace() call is made only if the preprocessor symbol TRACE is defined at the call site. Preprocessor symbols can be defined on the command line using the /D switch. Conditional methods must have void return type.


Personal tools