0

Does VisualBasic.NET or C# support conditional compiling? And inline functions (macros)?

When I talk about conditional compiling, I mean something like C/C++, where you do:

#ifdef DEBUG
    my_var = call_some_debug_function();
#else
    my_var = call_some_final_function();
#endif

And in the resulting compiled code, there is only the call to the call_some_debug_function or the call_some_final_function.

When I talk about inline functions, I mean something like C/C++ macros:

#define sum(a, b) a + b
...
total = sum(a, b)

And the resulting compiled code is:

total = a + b

Are these constructions supported by any of these .NET languages?

mHouses
  • 845
  • 1
  • 16
  • 35
  • Here's a question with answers explaining *why* C# doesn't support macros: http://stackoverflow.com/questions/1369725/why-arent-there-macros-in-c – Tanner Swett Sep 22 '16 at 16:16
  • You can cause a lot more havoc with macros in C and C++, C# certainly does not allow defining a buggy function style macro like that. But inlining optimization is not fundamentally different, the end result in machine code it is the same. – Hans Passant Sep 22 '16 at 16:18

2 Answers2

4

Conditional compilation is supported by both C# and VB:

C#:

#if DEBUG
   Foo();
#else
   Bar();
#endif

VB:

#If DEBUG Then
   Foo
#Else
   Bar
#End If

Macros are not supported in C# or VB as far as I'm aware... typically inlining is left to the JIT compiler.

Dave Doknjas
  • 6,126
  • 1
  • 13
  • 25
Jon Skeet
  • 1,335,956
  • 823
  • 8,931
  • 9,049
1

Yes it works but it's more something like:

#if DEBUG
    my_var = call_some_debug_function();
#else
    my_var = call_some_final_function();
#endif
CodingNagger
  • 965
  • 1
  • 10
  • 23