1

Ive just moved from VB.Net to C#. I dont understand why people are using it/prefer it as it is soo much more time consuming!

In VB.NET, you simply type your code and VB.NET formats is as you go, For example:

removes unneccessary whitespace, automatically puts in brackets, tabs blocks of code, automatically creates the NEXT, END IF, statements for blocks.

and the opposite/nuiances in C# if you change the name of an event handler it creates a new one, doesnt rename the existing one you must have the () at the end of a method

and im sure theres more.

Why is C# backwards like this? Surely there must be a way to improve productivity somehow. Any ideas or free tools out there?

Matthew Flaschen
  • 278,309
  • 50
  • 514
  • 539
Simon
  • 9,197
  • 13
  • 72
  • 115

4 Answers4

3

This has nothing to do with the language, and everything to do with the editor.

Regardless, the editor for C# in visual studio does support automatic formatting in several ways.

If you delete and reinsert the closing brace }, it will reformat/reindent automatically.

There are several menu items and corresponding keyboard shortcuts that will reformat code for you:

Ctrl+k+d - this will reformat the whole document.

Ctrl+k+f - this will reformat the selection.

There are also extensive refactoring capabilities - the rename refactoring will rename a member everywhere it is mentioned, even if it is in other projects.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
1

I'm using C# in Visual Studio 2008 and it behaves exactly as you describe. Pretty much every time I type a semi-colon or curly brace, it corrects all formatting within the context.

For example...

if     (myValue!= null) {
someValue = myValue;

If I type the closing curly brace, it turns into this:

if (myValue != null) 
{
    someValue = myValue;
}

All dependent on the style settings in Tools > Options

Fenton
  • 241,084
  • 71
  • 387
  • 401
1

Not automatic BUT.....

Use Ctrl+K+Ctrl+D to format document keystroke

Use Ctrl+K+Ctrl+F to format selection keystroke

From Visual Studio Format entire file?

Community
  • 1
  • 1
TJB
  • 13,367
  • 4
  • 34
  • 46
  • thanks for the keystroke combo. Does what i need... now if there was only some way to set up a timer... – Simon Jul 08 '10 at 09:22
1

Also there exits some so called code snippets. If you simply type if and press tab tab this will automatically result into

if (true)
{
}

setting the cursor directly onto the true.

Even better is the switch snippet. If you enter switch and press the tab twice you'll get

switch (switch_on)
{
    default:
}

where your cursor stands on switch_on.
If you now enter something meaningful like a variable name that holds an enum value (e.g. var color = Color.Red;) and press Enter it will automatically fill in all possible cases.

There are more code snippets available and some are very handy like foreach, try, prop, propg.

Oliver
  • 43,366
  • 8
  • 94
  • 151