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
  • 875
  • 1
  • 16
  • 36
  • 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,394
  • 1
  • 15
  • 28
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
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
  • 1,178
  • 1
  • 11
  • 26