Practical differences between C# vs Vb.Net
source link: https://www.michalbialecki.com/2021/03/22/practical-differences-between-c-vs-vb-net/?utm_campaign=practical-differences-between-c-vs-vb-net
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
For around a year and a half, I’ve been working with a project that was initially written in Vb.Net, but now newer code is written in C#. It’s a great example of how one project can be written in two languages, and code in Vb.net can reference C# and the other way round.
Vb.Net and C# are very similar and are compiled to the same Intermediate Language, but I found some differences more surprising than the others.
Simple differences
Let’s start with the obvious things. Vb.Net uses simple English in its syntax, while C# uses C-based syntax. Let’s have a look at the example.
And now the same example in Vb.Net:
It took me some time to get used to declaring types with As
keyword. Also If
statement needs Then
, but those are the things you write once or twice and it becomes a habit.
In my opinion, the biggest changes when comparing C# with Vb.Net are:
- we don’t use brackets for code blocks
- there is no need for semicolons at the end of the instruction
- variable names are not case sensitive
- and a few more
&& vs And vs AndAlso
All those simple differences are just differences in the language, but basically, code stays the same. One of my biggest mistakes when I wrote Vb.Net code was using And
as &&
operator in C#. The thing is that they are not the same.
Let’s have a look at the example. I’ll use two methods that check for null in the If statement and do a replace.
The difference between And
and AndAlso
is that:
And
will check statement on the right side even if the statement on the left side is falseAndAlso
will not check the right side statement if the one on the left is false
To show it clearly I wrote unit tests:
And the results are:
AndTest
fails, cause And
operator check both left and right side, which causes a NullReferenceException
. So if you want to check for null in the If
statement and do something else as well, just use AndAlso
.
Enforce passing a variable by value
Maybe it’s not a very significant difference, but I was surprised that it can be done.
Let’s have a look at a simple code and two tests:
We have a simple Add
method, where we increment a given value by 1. We pass this variable by reference, so it should be changed as well.
Notice that in AddByValueTest
we pass (a)
and this is the way how we can pass a variable by value. Note that it only works for value types.
And the results proves that it really works like that:
Converting enum to string
This difference stunned me when I saw it and it was a final motivation to write this post. I was doing a refactoring around Vb.Net with converting enums and my colleague was doing a review of the task and said.
– Hey, cool refactoring, but I’m afraid it might not work. – said the colleague.
– Really? I run that code and it was fine. – I said.
– You might want to check that again, I even wrote a unit test, to be sure. – said the colleague.
Let’s have a look at this test. I’m converting an enum to a string.
And when you run it, it fails, my colleague was right.
However, I was suspicious and the code I wrote was in Vb.Net, not in C#, so I wrote another test. But this time in Vb.Net.
And you know what? It passes!
So my original code was correct. However, Convert.ToString()
on an enum works differently in Vb.Net and C#.
Summary
Working with Vb.Net sounds like going back to the middle-ages, but in fact, I got accustomed to the Visual Basic syntax quite quick. The capabilities of both are very similar, cause it’s still the same code underneath, but I found C# slightly more concise and more intuitive as well. Maybe it’s because I preferred C++ over Pascal in school
There are differences, as you saw in this article, but they do not interfere with your daily work.
I hope you like this post, have a good day!
Related posts:
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK