Debug only constructors, not compiled in Release Mode

c# conditional(debug)
if debug not working swift
c# debug only code
c++ debug-only code
visual studio check if debug mode
ifdef release
visual studio c++ if debug
if debug unit test

Is there any way to create a "Debug Only" contructor which is not compiled in release mode?

The two solutions I've found so far are :

  • surrounds the constructor with an #if DEBUG. But it can be quite cumbersome if you have many methods/constructors
  • Use the ConditionalAttribute but it does not apply to constructors, nor to methods returning anything but void

Ideally, an attribute identical to Conditional but with an extended scope would be awesome.

Thanks for your help.

public class Foo
{

/// <summary>
/// Main constructor
/// </summary>
public Foo(){}

/// <summary>
/// Debug only constructor
/// </summary>
[Conditional("DEBUG")] //does not compile, wished behavior
internal Foo(bool dummy){}
}

EDIT

As it had been pointed by Cheng Chen & Erik Philips below, this question was an XY problem. The broader picture was : my main constructor loads heavy ressources from the Internet. But I wanted to debug with a fast loading constructor loading dummy values, testing only specific functions. I solved it by creating a dummy class instead of a dummy constructor of a working class. There may be better solutions to this problem.

So, bottom line is :

  • There is no attribute working on constructors similar to ConditionalAttribute
  • You can surround with a compiler directive #if DEBUG to prevent compilation of a specific constructor/method in Release Mode. You can as well group them into a partial class for better readability.
  • But, if you need such a constructor, think twice if there are no other options.

The requirement looks strange to me. I think you will get better answers if you explain from the very beginning, the current question is much more like an attempted solution to your original requirement.

To answer your question directly, we can use a factory here.

public class Foo
{
    private Foo(){}
    private Foo(bool dummy){}

    public static Foo FromNormal() { }

#if DEBUG
    public static Foo FromDummy(bool dummy) { }
#endif
}

#if DEBUG not working on a Windows Service release build, instead of the compile time constant to achive this. The code will allways be compiled into your code but only executed if you are debugging� My app was perfectly working on both iOS and Android in Debug mode, I tried to use Release mode, now my app is broken (bindings doesn't work anymore), of course i didn't change the code btw. I tried all solutions listed above including (tested on Android 7.0 - Samsung G S6 and iOS 10.3.3 - iPhone 5C) =>

Debug vs. Release, Debug/whatever.dll, and make a similar change in the release build. Thus const not only gives you compile-time checking, but can allow the compiler to� As expected, in release builds, it does print 1. However, in debug builds it prints 2. This is because the flutter tool injects the source location of Widget constructors into the code at compile time, so the code is effectively:

You can try using partial classes.

Basically you split the class into two files. One for normal implementation and the other for the Debug Only implementation, which is wrapped in the #if DEBUG compiler directive.

// Foo.cs
public partial class Foo
{

   /// <summary>
   /// Main constructor
   /// </summary>
   public Foo(){}
}

// Foo.Debug.cs
public partial class Foo
{
  #if DEBUG

  /// <summary>
  /// Debug only constructor
  /// </summary>
  internal Foo(bool dummy){}

  #endif
}

This way you can organize all methods, members, etc which will not be compiled in release mode in the same file.

ConditionalAttribute Class (System.Diagnostics), or attribute should be ignored unless a specified conditional compilation symbol However, its use on classes is valid only for types that are derived from Attribute. method should not be compiled into Microsoft intermediate language (MSIL) ConditionalAttribute is applied to the methods that are defined in the Debug� The class did not have a constructor. So, I added one that actually does not do anything and have no argument. It is happening only in RELEASE mode, not in DEBUG

Debugging Release Mode Problems, If you're reading this you've probably just discovered that you've made a classic developer mistake. You've been You can make your Debug compile more like a release compile by making the following changes: It is that it is not possible to debug in Release Mode. or use a constructor in XYZ struct. At times it is possible that some operations work fine in Debug mode but not in Release mode and vice-versa. Here are the some of the common errors that lead us to the above problem. 1. In general, the default value of an uninitialzed variable is garbage in Debug and zero in Release .

IDE Window: Compiler Options, 5 Code; 6 Compilation and Linking; 7 Debugging; 8 Verbosity It is only useful when you build the ppu files without Lazarus. build mode, because debugging will not work in the release build mode, and then only use the C Style Macros ( global); Constructor name must be init (destructor must be done)� Normally the Release build configuration creates optimized code and the Debug build configuration does not. The Optimize MSBuild property controls whether the compiler is told to optimize code. In the .NET ecosystem, code is turned from source to CPU instructions in a two-step process: first, the C# compiler converts the text you type to an

If You're Using “#if DEBUG”, You're Doing it Wrong – Peter Ritchie's , Yes, I could create a configuration and define DEBUG and RELEASE and do my i.e. try doing it without manually adding a conditional compilation symbol. You put this attribute on methods that[whose calls] may or may not be this method, the compiler will emit the call only if you compile in DEBUG.

Comments
  • Why do you need this? This is an XY Problem, that is you Think doing this will solve some other problem and instead of asking how to solve that problem, you want us to solve the problem with your attempted solution.
  • Alright, point taken. Bottom line : I need a fast loading constructor for debugging, whereas my regular constructor takes a lot of ressources. But I don't want this constructor to be available anywhere else. Internal maybe sufficient
  • Well why do you need a fast loading constructor (you're still telling us about your attempted solution, than the actual problem). What are you doing that is requiring a different constructor during debugging?
  • Loading a dataset with default values instead of their actual values (collected from the places all over the Internet)
  • You're right, done in an Edit. Btw, I think you are missing the return type for the Factory constructors: public static Foo FromNormal() { } and public static Foo FromDummy(bool dummy) { }. Is it a typo?
  • @XavierAM Thanks, I typed the code directly in browser, made a mistake.
  • Thanks for spotting the typo, I corrected the signature of the constructor.
  • Your answer actually explains well why it is not working but my question was : is there any workaround to get a "ConditionalAttribute like" behavior on constructors.
  • @XavierAM yes use a compiler directive, you cant change the attribute