C# (Sharp) Programming Language Question:

Download Job Interview Questions and Answers PDF

How do I do implement a trace and assert?

C# (Sharp) Programming Language Interview Question
C# (Sharp) Programming Language Interview Question

Answer:

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.

Download C# (Sharp) Programming Language Interview Questions And Answers PDF

Previous QuestionNext Question
How do I register my code for use by classic COM clients?How do I create a multilanguage, multifile assembly?