views:

1381

answers:

19
+9  Q: 

C#'s edge over VB

What in C#.NET makes it more suitable for some projects than VB.NET?

Performance?, Capabilities?, Libraries/Components?, Reputation?, Reliability? Maintainability?, Ease?


Basically anything C# can do, that is impossible using VB, or vice versa. Things you just have to consider when choosing C#/VB for a project.

+4  A: 

In early versions of VB.NET, the difference was more obvious, however with the current version, there are no significant differences.

VB supports XML literals directly in code, which is not supported by C#. C# supports unsafe code and VB don't. Technically these two are the biggest differences. There are many small variations but they are not important. I like C# more because I think the syntax is much less bloated. It's easy to recognize the blocks of code instead of seeing a bunch of keywords, but that's a pure personal preference.

Choose the one you and your team are more familiar with.

Mehrdad Afshari
XML literals in code is a HORRIBLE idea and an abomination to the language, IMO. I've never been a fan of VB, but that pretty much sealed the deal. Also, the whole _ to wrap a single line statement is pretty unnecessary.
Chris
_ is not unnecessary. It makes the ";" at the end of your "programming line" obsolete In 99% of the time you won't need a _
splattne
I agree with Chris, specially today, with LINQ, it's really dirty looking with _
Mehrdad Afshari
Ditto on the '_', but I think XML literals are a good idea.
kenny
Chris...have you actually *used* XML literals in VB .NET?
Kyralessa
+32  A: 

C# and VB are basically the same however there are some minor differences. Aside from the obvious grammar differences you have the following differences:

  1. C# can call unsafe code
  2. VB has optional parameters (Coming in C#4.0)
  3. VB is easier to use when making late bound calls (Coming in C# 4.0) This and number make 2 make using VB to do office automation much cleaner.
  4. VB has a bunch of "helper" functions and class like the My namespace; however, all of this are accessible to C#
  5. VB is case insensitive

C#'s syntax follow a similar grammar to c and java, which make it a much more comfortable transition from those languages, where as VB can be more comfortable to VB users. As far performance, and libraries or components they are nearly identical.

As for which one to choose, unless you have a need to do unsafe operations, then choose the language which is most natural to you. After years of being a VB developer I loved not having to write If yadada then.....End If if (yadaya){....} saves my carpal tunnel a few extra keystrokes (Which can then be used on answering SO questions)

Edit

Just learned one more difference btw C# and VB is that VB supports filtered exceptions so you could something like this pseudo:

try
{
   //do something that fails
}
catch(Exception ex when ArgumentException, 
      ArgumentNullException, FormatException)
{
  //Only handle these three types
}

This should not be confused with the ability to do:

   try
   { 
      //something that fails
   }
   catch(ArgumentException)
   { 
      //Log Error

   }
   catch(ArgumentNullException)
   {
     //Log Error
   }

In this case you are going to handle the exceptions differently in the VB world you could define one piece of code to handle multiple types of Exceptions.

Edit

Some more differences.

  1. VB's Is operator compares two objects to determine if they are the same it compiles to the CEQ IL instruction where as C# compiles to isinst IL. So the following are equivalent statements

c# if (foo is FooObject){}

vb If TypeOf foo is FooObject then

  1. Also as mentioned in the comments and I wish I could see them to give you credit but C# doesn't have a like parameter. You need to use the RegEx class.
JoshBerke
Woot, finally C# gets optional params? About freaking time! lol :D
Ben Daniel
no kidding I won't have to look at classes with 30 constructors covering all possible permutations of parameters
JoshBerke
Are you sure VB is case insensitive? I thought it was just Visual Studio that fixed the case of your variables and methods for you.
Rune Grimstad
I did this from memory and is 2:30 am so could be wrong let me google that
JoshBerke
http://petesbloggerama.blogspot.com/2007/04/c-vs-vbnet-case-sensitivity-what-to-do.html - Yes it is....and since C# is case sensistive as blog points out my.A and my.a in C# are two different things but will cause VB to puke
JoshBerke
Rune, VS fixes that to make code prettier and more consistent. But still `dataset` and `DataSet` are the same thing.
Mehrdad Afshari
@Josh re carpal tunnel: Intellisense and AutoComplete are so good in the VS2008 VB IDE that the actual typing for end-block statements is about nil if you do it right - it puts them all in for you. Also, IMO readability is better since the end-blocks are different, rather than just nested }s
ChrisA
I am programming in VS/VB.NET. Every time I switch to C# in VS I wonder how much better the VS Intellisense integration of VB.NET is. Isn't it?
splattne
I can live without optional parameters. I prefer overloads. If the signature is too complex and requires too many variations, it is a definite code smell.
joseph.ferris
@Chris: true I was trying to add some humor. Although for readability you may have a point but it's all subjective. That's one thign where MS has done a good thing is they have put both languages on equal footing. So now it's just your personal opinion
JoshBerke
@splattne: I find it to be quite the opposite. C# intellisense seems to be excellent whereas I can barely do anything with VB
PhoenixRedeemer
@phoenixredeemer: very strange. We have to exchange some screencasts to show our point. Maybe we're both failing somehow, doing something wrong. ;-)
splattne
Strange, but ur pseudo for VB is in c# :)
Binoj Antony
heh I'm a C# guy so that's sort of why.
JoshBerke
VB has the "is" operator, C# needs to use referenceEquals [== may be overriden]. Does C# have "like"? I don't remember.VB has 'handles event' and 'implements interface.method' modifiers for functions. Very, very useful.VB has optional () on property/function calls. Makes refactoring much easier.
Strilanc
C# will have optional in 4.0. C# also has an is operator. C# can also explicitly implement an interface as interface.method.
JoshBerke
I think he meant being able to implement interface methods using a different name (although i wouldn't do that personally)? I don't know VB, but i think IL has keywords that do just that.
Botz3000
C# can use multiple catch blocks to trap exceptions of different types `try {...} catch (ArgumentException ex) {...} catch (FormatException ex) {...}` etc. Not sure if it can use the `where` clause to capture multiple exception types in a single block like you demonstrated in VB though.
Matt Sach
+1  A: 

As I understand it, there are differences between the languages though they are minimal. I would suggest working with the language that you/your developers would feel most comfortable with. If they already have VB experience then I'd suggest VB.Net/vice-versa.

Though I prefer the terse syntax of C# personally. :)

Ben Daniel
if you like terse, try F#. It makes C# look like a long winded history professor
JaredPar
Reading VB code is like your friend is talking to you! It's just all "{" replaced with "Then"! No big deal.
TheAgent
+1  A: 

Since C# and VB.Net both compile into MSIL, both have nearly identical Performance, Capabilities, Libraries and Components. Reflection can deassemble MSIL code into either C# or VB.NET (or a number of other languages)

This basically leaves us with, C# looks a lot like Java and C++ which gives it more credibility.

James Curran
"credibility"? In what sense?
Jenko
@Jeremy Rudd "credibility" in the sense that there are a large number of programmers who believe that if a language doesn't have curly brackets and semi-colons then it must be a "toy" language. If you can't tell, I disagree.
Eric Haskins
I would have said "comfort" before "credibility". With C# I don't have to flex my brain much to jump from C++ to JScript to C#, or even to Perl which you can write in a similar way.
Mark Allen
@James: Just because they compile down to IL doesn't mean they're as equivalent as you imply. That's like saying that C# 2.0 and C# 3.0 are the same - all that's been added is syntactic sugar, but it's pretty *important* syntactic sugar.
Jon Skeet
@Mark: Wow. If having braces or not makes your brain stutter, your languages are too much alike. And your Perl most likely isn't very Perlish (thus: poor quality).
bart
@bart: I've written very C++-like Perl, merely by changing the formatting. How does that make it "poor quality"
James Curran
+3  A: 

My favorite feature of C# that VB doesn't have is the yield statement. It lets you easily return an lazily evaluated IEnumerable from a method.

Here is an article that covers it: http://msdn.microsoft.com/en-us/magazine/cc163970.aspx

Daniel Plaisted
+15  A: 

Street cred among geeks.

(And don't pretend that's not important!)

Nick Hebb
C'mon, what should a rational programmer care about some 'street cred' if he really just wants to get a job done well. Now if the Project Manager likes C# that's something else :)
Jenko
+21  A: 

I think this blog post by Kathleen Dollard provides an excellent overview to the question:

What a C# Coder Should Know Before They Write VB

and her first advice is:

1) Get over the respect thing or quit before you start. VB is a great language.

splattne
+1 Great link! Thanks and I prefer C#, time to learn a little more VB.NET.
kenny
I loves my VB.NET. That's a great post. +1
Booji Boy
+1 - Great post but hate the color scheme of her site (not that it matters)...
Dscoduc
+1  A: 

I think Josh gave a good rollup on the language differences.

Tooling Support

There are however also differences in how Visual Studio handles these languages. Code Snippets are easier to use from the C# editor and the refactoring is better also.

The VB-editor simplifies intellisense by not showing all options at all times.

I'm sure there are more things, but those are the ones that I as a C#'er (doing very little VB) have noticed.

Arjan Einbu
A: 

for the moment VB.Net has a very poor implementation of lambda expressions. Meaning that you can't do the neat things that you can in C#. Well you can, but it needs a very ugly workaround in most cases.

This is solved in VB.Net 10.0.

chrissie1
+3  A: 

VB has a better feedback on errors. In C# you have to compile more often to get all the errors in your syntax.

chrissie1
True. I've no religious objection to C# as a language - I use mostly VB.NET but C# is fine when I need it. But the VB IDE's precompile/intellisense/autocomplete system is much, much better. When I used C# on a contract earlier this year, using the VS2008 C# IDE was like going back 5 years.
ChrisA
With ReSharper this isn't a problem. I couldn't live without it now.
Andy McCluggage
Strange... Thats the way I feel when doing VB... (Going back 5 years)(We're probably missing different features that we take for granted in our day-to-day work...)
Arjan Einbu
Editing-time compiling is actually a problem for large projects as it significantly reduces IDE performance.
Jenko
@Jeremy - I never noticed that, and I have a 700000 Loc project in VB.Net.
chrissie1
I've heard this problem has been mitigated in VS2k8SP1, but we still haven't upgraded to it here.
R. Bemrose
@chrissie1 - Editing-time, only the currently EDITED FILE IS RE-COMPILED. I had a project with 20000 LOC in a single class, a single file, and believe me, even on my 3Ghz, it was slow to edit, even slow to type! I later split that class into multiple files using "Partial Class".
Jenko
@chrissie1 - ..and then it was workable. Now if you compare that to a large project with hundreds of SMALL CLASSES (upto 1K lines?) its a completely different process (more recursion?) for VB.NET's real-time compiler (VS's editing-time compiler actually).
Jenko
+4  A: 

VB.Net has a root namespace and C# has a default namespace which is not the same. Because when you have a root namespace in VB.Net it will always add that before the namespace.

For example: if you have a rootnamepsace in VB.Net named namespace1 and then you add this to your file.

Namespace namespace1
  Public Class class1
  End Class
End Namespace

then you will have to refer to it as namespace1.namespace1.class1

in C# if you have a default namespace called namespace1 and you this in your file.

namespace namespace1{
  public class class1{}
}

Then you can still just refer to it as namespace1.class1

chrissie1
This is an issue with Visual Studio defaults, not the VB.net language. This can be removed.
BradC
+11  A: 

Others have covered a lot of the differences - as has been said several times, they're nearly equivalent languages. A few differences which haven't been covered as far as I've seen:

VB9 has:

  • XML literals
  • Mutable anonymous types (urgh)
  • More LINQ support in the language (C# only covers a few operators)
  • A whole bunch of extra bits in the language which get compiled down to calls to the Microsoft.VisualBasic assembly. (C# prefers to be a small language with the weight of the .NET framework behind it.)
  • DateTime literals

C# 3 has:

  • Better support for lambda expressions: IIRC, you can't write a lambda expression with a block body in VB.
  • Iterator blocks.
  • Syntax for extension methods (instead of decorating the method with an attribute)

I suspect there is more, but I thought I'd just throw those into the mix.

Jon Skeet
The Microsoft.VisualBasic assembly is part of the .NET framework and a lot of C# programs use it.
Jonathan Allen
Add: C# works on platforms that don't support VB such as XNA/XBox 360.
Jonathan Allen
VB10 lambdas support blocks (multiple lines). This is for function and sub.
Steve Horn
+2  A: 

In C# you can have more fine tuned control over events/delegates. But you rarely need this.

There are a lot more code examples for C#.

In VB.Net it is (a lot) easier to use late binding. For example to COM objects (in C# from version 4.0).

  • I use C# for 90% in my projects
  • I use VB.Net for interop to Excel, etc

As soon as I know F# better, I will probably use it for the parts that F# is better suited for.

GvS
+1  A: 

Performance?

No difference, though VB historically uses a weird indexing in loops which means that you have to subtract 1 from the highest index most of the time:

For i = 0 To someArrayOrString.Length - 1 …

Though I doubt this impacts performance in any measurable way.

On the other hand, due to background compilation, VB actually compiles seemingly faster. Some people claim that this makes the IDE react sluggishly but I've never noticed that myself.

Capabilities?

In a few instances, C#'s yield statement is really useful. VB requires more manual work here. Also, lambdas are much better implemented in C#, especially syntactically. Consider these two statements:

Parallel.For(1, 10000, i => {
    // Do something
});

versus

Parallel.For(1, 10000, Sub() _
    ' Do something '
End Sub)

Besides the fact that VB can't do this yet, and that a comment at this place is forbidden, it's just more clutter and generally a no-go.

Libraries/Components?

Identical.

Reputation?

Not important. “Street cred”? Sorry. Not a factor. Get over it, Nick.

Reliability? Maintainability? Ease?

More or less identical. I claim that VB is easier but that may be biased and in any way, it's only marginal.

Konrad Rudolph
+3  A: 

The big advantage i see with C# is that the vast majority of open source projects, sample code and blog snippets are written in C#. Although it’s easy to convert these to VB.NET with tools (or your head) it is still a monotonous task for VB devs (like me).

Even if you write in VB.NET day to day, you will still need to be able to read C#

Technically, they share the same framework with same performance and memory characteristics and same type systems, so I find it hard to hard to split the two.

Most good developers should be able to shift between the two with a few days adjustment time.

My core frustaration lies with the hundreds of time I have written:

String lastName as String

and wondered why it never compiles!

nick_alot
It's painful, but C# is the king. I hate semicolon errors and case sensitivity. I think VB's verbosity makes it more readable.
Zack Peterson
+8  A: 

When you hit a problem, you'll often be able to Google for a code sample that shows how to solve it in minutes. This is a significant factor in improving productivity. When I was working with Delphi, I had to convert code samples from C into Object Pascal - doable, but tedious, i.e. lots of friction. So don't underestimate the fact that...

The vast majority of .Net code samples are in C# !

Mike Scott
True, and important, which is why every VB programmer should know C# – and vice-versa.
Konrad Rudolph
Jenko
+1  A: 

Here is another point not yet covered in this trail:

Higher Degree of Employability + Better (more) Dev Resources

Now I don’t agree with this point of view however:

There are more C# dev shops that VB shops

Some backward c# employers will put you at a serious disadvantage if you are stronger with VB. So maybe C# is the better choice here.

On the flip side, when you go to hire people for your C# project, you will be able to attract more candidates to iterview and get better skills as a result – I don't think detail should be overlooked.

nick_alot
+1  A: 

I have to say it's been my experience when using Google to search for examples or documentation that C# examples are of better quality than VB examples. This isn't to say that there aren't bad C# examples, but if you search for something like "dictionary drop down" adding C# will provide a better quality answer higher on the list.

This doesn't apply to examples that display both C# and VB code side by side.

greg
A: 

Free from old cruft, array declaration in C# is not padded with extra element. VB.NET array is padded with extra element, so legacy VB6 code migration is easier.

C# is more consistent than VB.NET

Button1.Color() = Color.Red
Button1.Color = Color.Red

I can't give an answer when one student asked me when to use parenthesis on properties of VB.NET. It's hard to give an insightful reasoning for this kind of misfeature.

Consistency with instance and static members. VB.NET allows accessing static members on instance e.g. yarn.Sleep(1000), this is a misfeature. http://stackoverflow.com/questions/312419/language-features-you-should-never-use

Michael Buen
No, array declaration is NOT "padded with an extra element." It just changes the *meaning* of the declaration. Instead of "declare this many elements" you're writing "declare an array up to this final element". Not the same thing as "padding", though it *can* be just as confusing....
RolandTumble