views:

70

answers:

4

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?

+3  A: 

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
True, the editor is the thing that is lacking here.
Simon
+1  A: 

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

Sohnee
+1  A: 

Not automatic BUT.....

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

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

From http://stackoverflow.com/questions/1435691/visual-studio-format-entire-file/1435702#1435702

TJB
thanks for the keystroke combo. Does what i need... now if there was only some way to set up a timer...
Simon
+1  A: 

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