14

Possible Duplicate:
How to force indentation of C# conditional directives?
Can visual studio automatically indent / format preprocessing directives?

In the following sample code there is a #if directive but notice the lack of indentation:

                else
                {
#if DEBUG
    Debug.Print(ex.Message);
#endif                    
                    return null;
                }

I know this is probably like this for some pure development practice, but honestly within VS.NET I do not care and would prefer it to align with my code. Is there a way to allow # directives be auto indented inline with the rest of the code in VS.NET?

Thanks!

Community
  • 1
  • 1
atconway
  • 20,624
  • 30
  • 159
  • 229
  • Possible duplicate: http://stackoverflow.com/questions/10548319/can-visual-studio-automatically-indent-format-preprocessing-directives, http://stackoverflow.com/questions/1321228/how-to-force-indentation-of-c-sharp-conditional-directives – Justin Helgerson Oct 26 '12 at 18:25
  • 1
    You should also consider using the ConditionalAttribute (http://stackoverflow.com/questions/3786827/alternatives-to-conditional-compilation-in-c-sharp) – Surfbutler Oct 26 '12 at 19:01

3 Answers3

5

I'm not sure you can do it with Visual Studio natively. You might have to use a plugin like StyleCop. See http://stylecop.codeplex.com/

I understand why you'd want the indentation done- because the conditional directives currently look quite messy. However, with the current indentation, the advantage is that it can be easily seen by someone who is reading your code. Since conditional directives can change the flow of your code by a great extent, it might be okay as it is right now. Otherwise, you have the plugin options :)

rizalp1
  • 6,346
  • 2
  • 17
  • 19
2

I tried to search something here:

Option -> Text Editor -> C#

but unfortunately it seems that visualstudio doesn't have anything built in which permit you to indent the preprocessor directives in that way. However googling I found this answer and it recommends to use StyleCop. Hope this helps you.

Community
  • 1
  • 1
Omar
  • 16,329
  • 10
  • 48
  • 66
0

I don't know of anything, but the # pragmas shouldn't be indented anyway, as they are not affected by the code. It's fine to indent the Debug.Print of course.

Surfbutler
  • 1,529
  • 2
  • 17
  • 38
  • 10
    I don't think indentation affects any code in C#. Its just for readability. – rizalp1 Oct 26 '12 at 18:33
  • 2
    Agreed. I meant that pragmas aren't affected by any previous lines of normal code, so shouldn't be indented as if they are. – Surfbutler Oct 26 '12 at 18:43