Visual Basic.NET features I miss in C#

During last year, I’ve migrated hundreds of thousands of lines from Visual Basic.NET to C#.

Luckily, most difficult part of this was convincing client, that it is necessary. There are great automated tools for this nowadays, Roslyn be blessed.

But I still believe, that Visual Basic is really not bad language. It had many good features that C# copied and there are still few left, that I will miss.

These are those features.

1. Event handling

You can define in function declaration, what it handles. This makes it very clear, what triggers the code.

VB.NET

WithEvents btn As Button

Sub Setup()
  btn = New Button()
End Sub

Sub ButtonClick(sender As Object, e As EventArgs) Handles btn.Click
End Sub

C#

private Button btn { get; set; }

void Setup() {
  btn = new Button();
  btn.Click += ButtonClick;
}

void ButtonClick(object sender, EventArgs e) {
}

2. Explicit interfaces

I know, that you can define functions in C# as explicit to certain interface. But then you loose ability to call it without casting it and it’s not very common anyway.

In VB, it’s always obvious when function or property belongs to Interface (or more) - which often means, it’s obvious how the code is triggered.

VB.NET

Interface IMyInterface

  Sub MyMethod()

End Interface

Class MyImplementation
  Implements IMyInterface

  Public Sub MyMethod() Implements IMyInterface.MyMethod

  End Sub

End Class

C#

interface IMyInterface
{
  void MyMethod();
}

class MyImplementation : IMyInterface
{
  public void MyMethod()
  {
    // Can't tell if necessary for Interface
  }

  void IMyInterface.MyMethod()
  {
    // Can't call without casting and is not common
  }
}

3. LINQ to XML

Yes, XML is becoming less relevant every day. I could write whole articles why I believe it’s not good, but for now, I’ll just miss the way VB handled XML.

VB.NET

Dim internalValue = "test"
Dim createXml = <MyXml value="value1" value2=<%= internalValue %>>
  <Item value="value" />
</MyXml>
Dim loadFromXml = createXml.<Item>.@value

C#

var internalValue = "test";
var createXml = new XElement("MyXml",
  new XAttribute("value", "value1"), new XAttribute("value2", internalValue),
  new XElement("Item",
    new XAttribute("value", "value")
  )
);
var loadFromXml = createXml.Element("Item")?.Attribute("value")?.Value;

5. Better guesses in Generics

Visual Basic allows me to specify just part of Generic attributes, that it needs. C# forces me to list them all (or none).

VB.NET

Dim dictionary = Enumerable.
  Range(0, 100).
  ToDictionary(Of Integer, Object)(Function(a) a, Function(a) a)

C#

var dictionary = Enumerable
  .Range(0, 100)
  .ToDictionary<int, int, object>(a => a, a => a);

4. No semicolons

I never realized how difficult semicolons are, until I started to teach programming.

It’s actually hard to sometimes explain, why there should or should not be semicolon. Not in VB, it just does not have them and semicolons is one of reasons, why C# is often not often being taught as first language.

5. Case insensitive

Shift on my keyboard was largely unused as Visual Studio just used declaration casing for me. Less typing, less work. You should not name multiple variables/functions same name with just different casing anyway.

6. Type after name

It’s becoming more common (see TypeScript), that data type is specified after it’s name. I mimics my way of thinking better. For example, my thinking goes “I need a name, that is a text”, not “I need a text that is name”.

VB.NET

Dim name as String

C#

string name;

Final word

I by now means try to convince you to use VB.NET for your next project. Choose C# all the way.

But if some of those features would find their way to C#, it would be really great.