in release mode the block is not compiled because it doesnt satisfy the conditional. Therefore the call to Trace.Write never happens even if it wasn't enclosed in #if(Debug)
Tested with the following code:
#define DEBUG
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DebugTest
{
class Program
{
[Conditional("DEBUG")]
static void LogLine()
{
Console.WriteLine("LogLine1");
}
static void Main(string[] args)
{
LogLine();
#if DEBUG
Console.WriteLine("LogLine2");
#endif
Console.ReadLine();
}
}
}
All should be "Yes". The #define DEBUG in the first line is confusing and causing the app to always run with this symbol defined.
All yes. #DEBUG is defined no all conditions are true. It doesn't matter in which mode you start the application. The #DEBUG word could be replaced by #SomeThingElse.
Tricky Question: But the anwser is YES,YES,YES confirmed. when no #define is used under Release mode then nothing would be traced, but now all are traced even when no Debug mode is on.
I just found the answer: Yes Yes Non
The code in the condition block won't be include in the assembly file output
Source: Go check in this book(Rob Miles - Exam Ref 70-483 Programming in C#-Microsoft Press (2018)) at page 231
Answer should be Yes, Yes, No.
If the application is running in RELEASE mode the call to this.LogData() would throw an exception prior to writing out LogData2 because the LogData() method would not be compiled in the RELEASE build, that is if it would even allow you to build in RELEASE mode due to that fact.
Sorry, scratch that. Answer should be Yes, Yes, Yes. I forgot about the fact that when using a Conditional attribute, the method and any calls to that method are not compiled.
"Applying ConditionalAttribute to a method indicates to compilers that a call to the method should not be compiled into Microsoft intermediate language (MSIL) unless the conditional compilation symbol that is associated with ConditionalAttribute is defined." ( https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.conditionalattribute?view=netframework-4.8 )
I agree and have also tested it.
- [Conditional("DEBUG")] checks if application is running in debug mode. So, if running in debug then "LogData1" is displayed.
- #if (DEBUG) checks if DEBUG has been previously defined, and it doesn't have to be in debug mode for it to be defined. So, regardless of mode, "LogData2" is always displayed.
The use of the word DEBUG is what is throwing people.
To those unsure try it yourself using any word. For example;
#define THISCANLITERALLYBEANYTHING
#if (THISCANLITERALLYBEANYTHING)
// do code stuff
#endif
and run in both debug and release modes.
A voting comment increases the vote count for the chosen answer by one.
Upvoting a comment with a selected answer will also increase the vote count towards that answer by one.
So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.
peter1994
Highly Voted 5 years, 3 months agobleasdal3
4 years, 10 months agosscooter1010
4 years, 10 months agociprian_alexandru
Highly Voted 5 years, 9 months agoJF_Me
4 years, 5 months agodemoinq
Most Recent 4 years agoBintang0
4 years, 1 month agoHgstExam
4 years, 3 months agothiemamd
4 years, 4 months agothiemamd
4 years, 4 months agothiemamd
4 years, 4 months agoSully_2020
4 years, 8 months agoSully_2020
4 years, 8 months agosscooter1010
4 years, 10 months agorhysabray
4 years, 9 months agofounderDev
4 years, 10 months agoBintang0
4 years, 1 month ago