Interview Questions

How do I do implement a trace and assert?

C# Interview Questions and Answers


(Continued from previous question...)

55. How do I do implement a trace and assert?

Use a conditional attribute on the method, as shown below:
class Debug
{
[conditional("TRACE")]
public void Trace(string s)
{
Console.WriteLine(s);
}
}
class MyClass
{
public static void Main()
{
Debug.Trace("hello");
}
}

In this example, the call to Debug.Trace() is made only if the preprocessor symbol TRACE is defined at the call site. You can define preprocessor symbols on the command line by using the /D switch. The restriction on conditional methods is that they must have void return type.

(Continued on next question...)

Other Interview Questions