I’m having trouble the same instance of a C# class being used in multiple places. I’d like to be able to tell at a glance in the debugger which instance is which, so I’ve added a field called DebugUID like so:
public readonly string DebugUID = Guid.NewGuid().ToString();
and I’ve also added the attribute [DebuggerDisplay("DebugUID= {DebugUID}")] to the class.
I’d like the compiler to ignore the DebugUID field in release mode. If it were a method, I’d add [Conditional("Debug")] before it, but it won’t let me do that for fields. How do make a field be debug-only in C#?
C# has conditional compilation features analagous to those of C/C++. You can use the
#ifstatement with a compilation directive likeDEBUGto accomplish this.Since by default only “Debug” configurations define the
DEBUGdirective, this will only compile in Debug mode. In Release mode the code inside the#ifwill be ignored.