r/csharp • • Feb 12 '19

An Exhausting List of Differences Between VB.NET & C#

https://anthonydgreen.net/2019/02/12/exhausting-list-of-differences-between-vb-net-c/
122 Upvotes

43 comments sorted by

6

u/[deleted] Feb 13 '19

I started off with Visual Basic but quickly realised that the jump to C# wasn't that huge in terms of complexity. There's tons of amazing educational content for C# making it definitely one of the easiest and most powerful languages to learn!

10

u/[deleted] Feb 12 '19

I did a project with MS about conversion from VB6 to VB.net many many years ago. It did not go well.

Make the leap, VB.Net does you no favours.

14

u/chucker23n Feb 12 '19

I indeed wouldn't recommend converting from VB6, because you're ultimately stuck with the wrong paradigms and idioms.

That said, VB.NET as a language is fine. It even has some cool features C# lacks, like XML literals, and it had optional parameters long before C# did. It also has some anti-features like automatic default form instance (I wish this were a compiler option). So does C#, though; for example, making / a C-style integer division was IMHO a bad call, and VB gets this one right.

Overall, I prefer C#, but recent versions of VB are vastly underrated. It matters more that you understand the idioms of the ecosystem, like data binding and LINQ (which, too, VB does just fine), than any particular language.

14

u/FenixR Feb 12 '19

I think most of the hate from VB.net comes inherited from the VB6 era.

7

u/tweq Feb 12 '19 edited Jul 03 '23

4

u/ImKrypton Feb 12 '19

there is also native support for CSV in VB.NET but not in C#. You need 3rd party library or reference Microsoft.VisualBasic assembly for that.

-1

u/Revocdeb Feb 13 '19 edited Feb 13 '19

What does VB.Net do with csvs that C# cannot natively do?

The solutions here look totally doable in C# .Net

Edit: Too bad I got downvoted, the comment I replied to is completely false.

4

u/[deleted] Feb 12 '19

I converted a 80k line project from vb6 to .net, it was quite easy.... Then we refactored to fix the pradigms and idioms...

I dream, my boss said we are not upgrading to .net and I quit 3 months later.

1

u/[deleted] Feb 12 '19

Actually I sort of agree. We are talking about people who rock at language design though :)

3

u/Otis_Inf Feb 13 '19 edited Feb 13 '19

Of course it's a list of nice things to say about VB. But I'm sorry, it really is a broken language. Using () for both method call and array indexing for instance makes reading code hard:

 Dim i = foo(j)

what's foo ? Method? Array? unclear.

 Public Overrides Overloads Function SomeFunction() As SomeType Implements ISomeInterface.SomeFunction

really... ? (ok, granted, 'overloads' isn't necessary in modern versions of VB.NET)

From my work on a full Linq Provider, I've learned (sadly, the hard way) that the VB.NET compiler generates different expression trees from the same lambdas (so the lambda is the same in VB and C# semantically): they introduce needless coalesce expressions for elements which can't be null, the anonymous types are named differently (but granted, that's minor), and what's pretty terrible is this: if you compare two string typed elements in a linq query, it produces a CompareString(a, b, false)=0 expression. This method is a Visual Basic method defined in Microsoft.VisualBasic.CompilerServices.Operators. However you have to check for that and add special code for that specific call, a clal which is completely unnecessary, remember we're talking expression trees.

The compiler also generates ConvertChecked expressions for To* method calls (again in the special compilerservices assembly), which require special handling. This is unnecessary as we're talking expression trees: these are handled by a handler, they're not ran. It's only a burden to the people writing a Linq provider, and serves no purpose.

Other issues:

  • Using 'And' in a linq query is different than using 'AndAlso'. You again have to add special code in your linq provider to deal with that.

  • Using e.g. Where f.SomeCollection.Count() = 10 in a linq query creates a memberaccess expression for the Count property, not a call to the Count() extension method. Why, no idea... But it only leads more special cases to deal with.

  • And don't get me started on the GroupBy differences in linq queries between VB and C#: the expression containing the projection in a linq query generated by C# with a Group by is in a separate Select call. In VB it's inside the groupby. If you used a compound grouping (using an anonymous type), VB generates specially named elements called eg. $VB$Key. You have to write a special case for handling these expression trees to be able to handle group by if the query comes from VB.

I have more but it's so depressing. So much time wasted implementing special cases for for VB specific expression trees. Yeah VB... lots of fun. I seriously don't know why people would pick it over C#, altho C# isn't always great either (break in a switch case... argg). C# at least tries to be consistent at most times and doesn't have illogical design decisions all over the place.

2

u/NihilCredo Feb 14 '19

if you compare two string typed elements in a linq query, it produces a CompareString(a, b, false)=0 expression. This method is a Visual Basic method defined in Microsoft.VisualBasic.CompilerServices.Operators. However you have to check for that and add special code for that specific call, a clal which is completely unnecessary, remember we're talking expression trees.

The article explains this point (it's #63). The TL;DR is that VB supports optional case-insensitive string literal comparison, which is implemented through the extra parameter to CompareString.

I think of Option Compare Text as more a back-compat option but I get why it existed in the first place. There are a lot of contexts where you want string comparisons to be case-insensitive.

In fact, most contexts where I use strings I want them case-insensitive.

Essentially all contexts outside of passwords and encryption keys. But everywhere else I don’t want my typing a literal lazily to affect my results. Yes, I’m that monster who uses case-insensitive collation on SQL Server because I value my productivity. And if you consider that VB’s history includes not only VB6 but VBA for Office products like Excel and Access and VBScript for Windows and that one web browser that one time, … ain’t nobody got time for case-sensitivity. That said, I accept that .NET is a generally case-sensitive API and I don’t use Option Compare Text because it only affects the language. If there was an setting that affected all the .NET APIs too I’d flip that sucker on and never look back.

1

u/[deleted] Feb 13 '19

[removed] — view removed comment

2

u/Otis_Inf Feb 13 '19

Unless you are literally talking about an array, indexing is a method call.

That's not clear, right? hence my point :) I might be talking about an array, it might be a method, it might be a list...

1

u/[deleted] Feb 13 '19

Thank you codebasedgod

1

u/LoKSET Feb 13 '19

Phew, I'm exhausted.

1

u/[deleted] Feb 12 '19

I think vb is a lot easier to read than c# is, but C# gets all the love.

3

u/EinBohredDude Feb 13 '19

C# is a very balanced language between spoiling the shit out of you and also being technically workable.

0

u/[deleted] Feb 12 '19

[deleted]

20

u/[deleted] Feb 12 '19

When I began teaching myself to code about 10 or so years ago, I started with VB.NET because:

  1. It was a quicker learning curve,

  2. I already had a little bit of VBA experience, but not much else

  3. The company I worked for, at the time, built any web apps in VB (this has fortunately since changed)

I was told by more than one developer that (being self-taught), I probably did it the right way, but I ought to start learning C# if I wanted to progress in my career.

That was about 10 years ago; now, I can't stand the sight of VB. *shudders*

1

u/pgmr87 The Unbanned Feb 13 '19

I've only had two jobs since graduating and both jobs have these legacy VB6 apps that were converted to VB.NET (I call the result VB++). These VB++ apps use Winforms and are some of the worst things I have ever seen. I get that they started in VB6, but even VB6 had best practices, design patterns, and more, not to mention general programming best practices (like naming variables as descriptive as possible i.e userName, not temp). These apps are so poorly designed that making simple changes is a couple hours of work and, no matter how careful you are, you are very likely to induce bugs. So frustrating.

1

u/Dkill33 Feb 14 '19

I had a similar learning path. I liked VBA at the time because I wasn't a developer but I could do some really cool things with VBA. Now that I have learned c# and a few other languages I can't stand VB anymore.

12

u/[deleted] Feb 12 '19

At some point they should have realised pretty much all the good developers use C# and the conversion path is almost free. I cant think of a logical reason to continue to use vb.netin any active project.

0

u/[deleted] Feb 13 '19

[deleted]

12

u/Dorambor Feb 13 '19

Oof, as a presenter that would have crushed me

But as a developer I would have done the same

1

u/[deleted] Feb 14 '19

As a developer I would have told everyone to get the hell out while they can... Then left.

8

u/anon_smithsonian Feb 12 '19

But I have serious difficulty understanding why anyone would want to pick up (Edit: as a newcomer to .NET) VB.NET instead of C# at this point.

For me, I started with VB.NET because my experience was primarily with VBA so the learning curve from VBA --> VB.NET wasn't as steep as it would have been going from VBA --> C#, since I didn't have to try and relearn all of the syntax and keywords and everything else on top of learning the framework.
 
Once I was a lot more comfortable with the .NET Framework and differences in design patterns and approaches (and came to appreciate the value of lambda expressions), I made the transition to C# (r => ... is so much nicer to write than Function(r) ...), and the learning curve from VB.NET --> C# was a lot nicer because it was primarily just having to break the old muscle-memory stuff and relearn syntax (and to stop forgetting to type ; at the end of every line all of the time).
 
I imagine it might not be as difficult for people who are already experienced in using multiple languages regularly, since you'll already be used to switching syntaxes, but the progression from VBA --> VB.NET --> C# felt a lot more natural to me and I feel like I spent less total time struggling to figure out how to do something than I would have if I tried going right from VBA to C#.

 

But I think it ultimately depends on your reasons for learning .NET in the first place. If you're doing it for employment/career purposes, C# is where you want to be. If you're basically just experimenting and already have a decent understanding of VBA or VB6, then with VB.NET you'll at least be able to hit the ground running a little faster because you won't have to relearn syntax and can also bask in the glory of the multitude of quality-of-life improvements that VBA lacks (like implicit variable declarations and single-statement declaration/assignment (e.g., Dim SomeVariable = 1)).
 
For the majority of people, though, it's almost always going to make more sense to go right to C# instead of VB.NET.

6

u/falthazar Feb 13 '19

This is exactly the same thing I did! VBA -> VB.Net -> C#

I really liked VB.Net coming from VBA, and it is a good language. But there are just so many more resources for C#, and once you get started you realize how much nicer it is to write.

2

u/sooka Feb 13 '19

I did the opposite, coming from VBA I chose C#.
I'm still using VBA on Excel, but I was so tired of it that couldn't choose VB.

From my perspective it is and was the best decision.

3

u/StopThinkAct Feb 12 '19

It's made even easier to convert with the MSIL, just archive your vb.net code, grab the dlls and import them to your shiny c# libraries. Boom, converted.

1

u/pgmr87 The Unbanned Feb 13 '19

I haven't had much luck with conversion tools like Telerik. If the library was written in VB.NET from the start then conversion is usually simple and fine. However, many of our apps that use VB.NET were originally written in VB6. I've never been able to successfully convert such VB apps to C# -- there's always been errors.

3

u/BCProgramming Feb 13 '19

If you liked (or have to convert something from) VB6, then sure... I can see the attraction to VB.NET.

As somebody who went from VB6 to C#- I felt it was the opposite. My first "real" programming language that I developed a level of comfort using was Visual Basic 2.0, so I had been using VB for a while.

But VB.NET was different enough that it really was a brand new language that was "mimicking" what I was already familiar with. Breaking changes throughout the language made it rather untenable to "migrate" a project since significant portions of anything non-trivial would tend to need rewritten anyway.

Considering that, C# seemed almost obvious to me. I'm going to be learning a new language anyway, so I may as well go for the "native" CLR language instead of "Visual Fred" (as people used to call it).

Interestingly, while I've not developed anything in VB6 for years, I have a few utilities I use regularly (like a command line expression evaluator) that I wrote in VB6 over 10 years ago now.

2

u/ASemiAquaticBird Feb 13 '19

I kind of fell into a development role, because I had created a spreadsheet system using VBA that was very useful, but severely limited in expansion. Never had written a line of code before other than VBA, needed to create a desktop application that used a SQL server to expand, so VB.Net was the most seamless transition.

Within 6 months I was slapping myself in the head for not going the route of C#, but at that point the app was already in use, with a backlog of interfaces and functionality that it took two years to catch up. I wanted to re-write, but it would be a complete re-write of core architecture.

Fortunately there were several other applications that I had to develop during this time period, which I did in C#. Now that the application is stable and has no backlog, I'm planning on a full re-write in C#, as well as making it web based instead of desktop based.

0

u/Zanthious Feb 13 '19

because vb.net is easier fir lazy programmers. you dont even need to convert database data and just assign it to a string or int and it just works. then u get in the habit and hate any language that expects you to do it right

0

u/[deleted] Feb 12 '19

[deleted]

13

u/anon_smithsonian Feb 12 '19

So shift to C#. You also get more NuGet packages.

What? NuGet packages are language agnostic; it doesn't matter if you're using C# or VB.NET as long as the targeted .NET Framework versions are compatible.

0

u/[deleted] Feb 12 '19

[deleted]

4

u/[deleted] Feb 12 '19

You actually sounded like your taking the piss out of vb developers...

0

u/[deleted] Feb 12 '19

[deleted]

5

u/Wings1412 Feb 12 '19

VB.net has an awfully syntax, and I say that as somebody who has done a lot more VB.net than c#. Once you get used to the c syntax, VB is just a pain in the ass. You spend forever writing out overly verbose statements that are a handful of chars in c# (e.g implements vs :).

4

u/zulelord Feb 12 '19

Intellisense makes your argument void, no? Do you actually type out 'implements'

3

u/Wings1412 Feb 13 '19

I should have made it more clear that the above is my opinion and if you prefer VB.net then more power to you!

Although i'm not sure why my argument is void because of intellisence... that was just one example, and even with intellisence you have to type more than with c#, also that was only one example.

You are correct that a lot of the tedium is helped by tooling though, for example a few years back i had to maintain a VB6 app which has very poor tooling and compared to that VB.net is the nectar of the gods.

For me though; I find that C# is just easier to read and write.

0

u/infodump Feb 12 '19

You can say without doubt it's easier for you. I have a hard time doing some things in VB cause they just make no sense and are super easy in c#. Also new forms are literally identical in c# vs VB have you ever written in c#?

4

u/8lbIceBag Feb 13 '19 edited Feb 13 '19

I have a hard time doing some things in VB cause they just make no sense and are super easy in c#.

As someone who programs in both. This goes both ways depending on the task.

The day I can mix vb, c#, and msil) in the same project would be ecstacy.

2

u/pgmr87 The Unbanned Feb 13 '19

Yeah, I am not sure why multi-language projects haven't been officially supported yet. I'd like to be able to write mostly C#, use VB for XML stuff, and F# for data processing, all in a single project.