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 cre开发者_如何学编程ates 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?
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.
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?
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
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
.
精彩评论