Please note that I am aware of Debug.Print - the Console.WriteLine is a very simplified example of what I'm trying to do.
Is there a way to have a single line of code which exists only in Debug mode, which does not appear in Release at all?
I have some commands which help me debug the execution of a performance-critical section of code, and I have placed a large number of them all over the function in key places.
Here is an example of what I've done:
using System;
public class C {
public Object _obj = new object();
public void M()
{
Alpha("This goes away in Release");
Alpha(_obj.GetHashCode() + "...but this doesn't");
#if DEBUG
//But I don't want this three line deal.
Alpha(_obj.GetHashCode() + "...of course this does get removed");
#endif
}
public static void Alpha(String s)
{
#if DEBUG
Console.WriteLine(s);
#endif
}
}
The issue is that in release mode, the compiler recognizes that the first call does nothing in release mode, and removes it. But it does not do that in the second call. I know this because I have tested it in SharpLab: https://sharplab.io/#v2:EYLgHgbALANALiAhgZwLYB8CQBXZBLAOwHMACAZQE9k4BTVAbgFgAoTAB22ABs8BjE3lxTISAYRIBvFpnace/APLAAVjV5wSAfQD2KkgF4SBGgHcSu1eoAUASiatZ3PiQBu2vABMSAWVslpmFIOmACCXGwAFohWAEQAKhF4IkTaNCKIJogUJIQkAEo0XDQoNDF2AaHhUVY6KgB0AOI0cAASKBGi2h40fgDUJDF1Q8DYGnCJIh6pyAQAAgC0AIxwZfYymBUAxHgAZiQAIgCiAEIAqg3+wZgA9NfHoyQAkiRTc0samQRjEyTjAE40GgkHjGF7FLh1CqVSLRWrKRrNNrIDpdHo2Ej9QZDbR7XjabB/ZBA8ZJF7TEhEZokAGobQuGgeVZbGgEDy7KEBAC+AQCHCc/GoiDgzjcnhIYRhVjIcD+hFIyBsASC622eyOZwaUM6BGQ2iKdQA6rLaAAZQg9BVrGSbFlsnZc6ScoA==
Is there any way of avoiding the three-line version?