Creating Blocks (e.g. Classes, Methods, If…Else, etc.) (+10 VB.Net)

Both VB.Net and C# have automation built in when it comes to creating blocks. However, C#’s implentation is quite a bit less robust.

Creating a new block


To Create a new class, method, and property in VB.Net, you could type:

public class TheClass<enter>
public property TheProperty as string<enter>
public sub TheMethod<enter>

Result (including important cursor positions):

Public Class TheClass
Public Property As String
End Get
Set(ByVal value As String)
End Set
Public Sub TheMethod()
End Sub
End Class


To Create a new class and method in C#, you could type:

public class TheClass {}<home><enter><up><tab>
public String MyProperty {}<home><enter><up><tab>
get {}<home><enter><up><tab>
set {}<home><enter><up><tab>
public void TheMethod() {}<enter><home><enter><up><tab>

or (using shortcuts)

public class TheClass {}<ctrl-enter>
public String MyProperty {}<ctrl-enter>
get {}<ctrl-enter>
set {}<ctrl-enter>
public void TheMethod() {}<enter><ctrl-enter>

Result (including important cursor positions):

public class TheClass {
public String MyProperty {
get {
set {
public void TheMethod() {

C# Inconsistencies

If there is a syntax error (e.g. unclosed block) near the new block, it will not be auto-completed or auto-formatted.


Luckily for C#, <ctrl-enter> exists. Otherwise, you would have to type <home><enter><up><tab>. However, it took me a while to find this shortcut and get used to using it.

Without the shortcut, there are 6 extra keys to hit. With the shortcut, there are only 2 extra keys plus a combo key instead of just enter (so 3 extra keys).

On top of the extra keys in C#, you must make sure all the syntax is correct or else auto-complete will not happen.

Auto-formatting of existing blocks

This is where C# really shows its inferiority. Once stuff gets moved around, the only way to get back to standard formatting is to make sure there are no syntax errors and then hit <ctrl-K><ctrl-D>. Well, that or manually move it… but are we using an IDE here or Notepad?

There are lots of reasons this C# will not auto-format your block for you even after hitting <ctrl-K><ctrl-D>. For example, if a previous block is unclosed, or if there is a syntax error somewhere nearby, such as missing a ‘;’ inside the block.

Feelings on the Matter

I find myself constantly hitting <ctrl-k><ctrl-d> (shortcut for format document) while programming in C#, since the formatting of the document becomes non-standard after a bit of editing.

Basically, VB.Net’s philosophy is: auto-format all the time, unless you are talking about a line continuation (using an underscore character).

C#’s philosophy seems to be: auto-format only when finishing a block and there are no syntax errors to speak of nearby.


5 comments so far

  1. hobbit on

    you are ignoring the code snippits used by almost anyone doing c# in

    (e.g. prop)

  2. anup singh binjwe on

    if else based more and many-many example seeing your side

  3. Nate on

    thanks for the ctrl+KD tip, that is what I was looking for

  4. Jon_w on

    The CTRL+Enter does’nt work for me – guess it’s dependent on Visual studio environment settings
    * vb setting (horror CTRL+Y removes line instead of redo : yuck! +o(
    * c# setting
    * genereal setting ( used by me)

    Indeed, a kindred spirit. From a perspectiva I have loathed the extra typing in c#.

    My salvation was to use codesnippets.

    Class+TAB (Type in Class name in green snippet field) + Enter
    Automagic {} and TAB indentation. Curson in between {}

    ex: Class+TAB Demo+Enter => yeilds:

    Class Demo()
    | Cursor here…

    Now we need a constructor…

    Code snippets (again) to the rescue.

    ctor+Tab (yeilds)
    public Demo()
    | Cursor here 😀


    And of course properties ( as stated in previous comments)
    with prop+Tab (default type int- changeable) and Property name


    For one as me (code flow) is more important than {} and ;

    I have created a macro for CTRL+ENTER that I have come to use in c# mode…
    CTRL + ENTER yields ; and newline with correct indentation.

    In my code thought I think I’m done with that row…
    Especially since with a different locale than english the ;-char is’nt always easy to single “hit”. On my keyboard it’s the key next to M with a required SHIFT so for a new line in c#
    SHIFT+, and then ENTER : yuck!

    The macro result is more like in MSN when one want to make a new row WITHOUT sending it to the person.. one could hit CTRL+ENTER…
    and continue…the “flow”

    nerdy? 😉

  5. Ben Von Handorf on

    I realize this is old, but in VS 2008 you may want to investigate CTRL-K, CTRL-S for “Surround” in C#. You can then pick all sorts of surrounding blocks (if, while, using, #region, etc). Personally, I find this MUCH more handy than typing If in VB.NET and then having to move the code into the automatically generated THEN block.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: