VB.NET compared with C#

Silberzorn

Newcomer
Joined
Jun 27, 2003
Messages
2
Location
Australia
Hey all,

Just a question about something I have been wondering about.
In my VB.NET book I read that one of the aims for the new .NET
framework was to place all programming languages supporting the CLR on the same footing. The book also says that regardless if a program is written in C# or VB.NET it will be compiled to similar MSIL. So my question is, is there any advantage (as in speed, flexibility, access to low-level hardware etc) in learning C#, when VB.NET is such a user friendly programming language?

Thanks,

Silberzorn
 
I must admit, I quite dislike VB.NET. I prefer C style syntax I guess. There are other reasons, though:

  • VB.NET provides all kinds of backwards compatability features with VB6 that allows you to do all kinds of messy stuff; C# doesn't have that (that's a good thing, it keeps your code clean :))
  • C# allows more control over your code structure. VB.NET removes all the excess whitespace from your code, doesn't have block comments and allows you to carry on code with blank lines, while in C# you can lay it out however you like
  • The only thing I don't like about C# is adding events to classes; you need to define both a Handler delegate and a Args class to call an event with custom parameters, where in VB it's a simple Event line.
If you are just starting out, or are converting to .NET from VB6, I would really recommend starting with C#; it forces you away from the VB syntax (so bad habits won't persist) and removes you from all those awful compatability functions .NET provides.
 
To add to VolteFace's list
  • VB has poor bitwise manipulation
  • VB doesn't automatically short circuit logic evaluation, you need to use the new keywords like orelse, andalso
  • C# also has operator overloading (if you need it that is)
 
You can circumvent Volteface's point 1 by only using the .Net classes ie. String.Length instead of Len()
You can circumvent point 2 by disabling the Pretty Listing (reformating) of code option in the Tools>Options Dialog under Text Editor>Basic>VB Specific

VB supports very simple Bitwise ops using Or and AND and I'm not sure what "operator overloading" is
 
VolteFace said:

If you are just starting out, or are converting to .NET from VB6, I would really recommend starting with C#; it forces you away from the VB syntax (so bad habits won't persist) and removes you from all those awful compatability functions .NET provides.

Are you saying coding in VB.NET is bad? :eek:
 
Although you can use 'Or' and 'AND' to do bitwise stuff the same keywords are used for boolean logic as well, if there is a situation where either bitwise or boolean could be applied there doesn't appear to be a way to force bitwise (try enums with the <flags> attribute for example)
 
VB gained >> and << bitwise operators in the latest version, although it still can't work with unsigned types so their usefulness is limited.

VB has optional parameters, C# doesn't
C# has operator overloading, VB doesn't
VB has late binding, C# doesn't
C# supports unsafe code (where you can work with pointers), VB doesn't

Although late binding is generally a bad idea, it has its uses. There is no CreateObject in C#, which makes working with unknown COM types impossible without heavy use of reflection.
 
And C# has some goofy syntax, just like VB.

For example (just found this the other day). Normally you use one pipe (|) to do bitwise compare, and two pipes for logical boolean compares. So:
C#:
bool a = true;
bool b = false;
if(a || b)
{
    // Do something...
}
else
{
    // Do something else...
}

But, we found out that in C# you can actually use one pipe for logical boolean compares (unlike C++):
C#:
bool a = true;
bool b = false;
if(a | b)
{
    // Do something...
}
else
{
    // Do something else...
}

It's not big, but it's a little sloppy in that we had two different developers working on a form and each was using a different syntax for it. Good thing we do code reviews :)

-Nerseus

PS Yes, I know this has nothing to do with VB/C# differences. Just giving some fuel for the VB developers who get hit with (VB is sloppy, let's you do sloppy things, blah blah).
 
Nerseus:
I'm not to familiar with the inner workings of .NET, but isn't the value of true 1, and the value of false 0? So.. 1 OR 0 = 1, which is true. Thus the syntax (yet cryptic) looks correct (at least to me). It seems a little odd that C/C++ wouldn't act in a similar fasion.
 
Technically, as far as C++ is defined, false is 0 but true is any number except 0 (usually 1 in C++ or -1 in VB). But the single pipe is supposed to do bitwise ORs, such as when using enums with flags:
C#:
[Flags]
public enum Seasons
{
   None = 0,
   Summer = 1,
   Autumn = 2,
   Winter = 4,
   Spring = 8,
   All = Summer | Autumn | Winter | Spring,
}

Seasons season = Seasons.Winter | Seasons.Spring;
if((season & Seasons.Winter) == Seasons.Winter)
{
    Debug.WriteLine("true");
}

But if you try the following, it won't even compile (as I'd hope):
C#:
int a = 4;
int b = 7;
if(a | b) ...

-Nerseus
 
But the single pipe is supposed to do bitwise ORs, such as when using enums with flags:

Perhaps my post wasn't clear, or perhaps I'm not understanding what you're trying to say. Like I said I don't know the inner workings of .NET, so I'm probably just being stupid and misunderstanding everything. I tend to do that.

Either way let me see if I can clear up what I said above;

bool a = true;
bool b = false;

Bits in both values;
a = 00000001
b = 00000000
You or these values and you get
= = 00000001

Is the result not the same as the value true?

if (a | b) should provide true as the result. I would think that this would be the same for if (00000001) or rather if (1).

This example;
int a = 4
int b = 7

a = 00000100
b = 00000111
Or the values..
= = 00000111

so if (a | b) is the same as saying if (00000111) or if (7). I would assume that this of course would give you a compile error, as 7 is neither true or false, which if () expects.

If this is correct (probably not, but this is what I'm getting at) then the syntax if (a | b) is correct. In C++, if false is 0 and true is not 0, then I see no reason why this wouldn't work under C++ either. Except of course it would consider if (7) as true, which is probably a bad thing. :)
 
AndreRyan said:
You can circumvent Volteface's point 1 by only using the .Net classes ie. String.Length instead of Len()

I know that, but C# doesn't even provide those options, so VB.NET can be confusing to newbies who don't know what is considered "correct".

I mean, there's nothing wrong with coding in VB.NET as long as you know what you're doing, otherwise you can get bogged down in the "extra" options VB provides you which should generally be avoided.
 
wyrd: Your example would (and should) work perfectly in C++, as the bool values are really ints (C++ has overloads for BOOL and values like TRUE and FALSE - there is no native Boolean datatype).

But in C#, it's a bit "sloppy" to have the | operator be a bitwise OR in case and a logical OR in another, based on datatypes. C# was supposed to be more type safe but I consider the single pipe a "slip". Since C# doesn't define what int value a bool type has (0, 1, -1, etc.) internally, you shouldn't be able to use an operator that *normally* is used for bitwise operations (which only work on numbers, not abstract datatypes like a boolean). The fact that it does work is only a convenience in case you forget which to use (one pipe or two). Since most people would call this "sloppy" and choose to only use the two pipes, I thought it worth mentioning (since we're on the topic of "sloppy" coding in VB.NET versus the "clean" coding of C#).

-Nerseus
 
Ohhh.. I gotcha.


Nerseus said:
wyrd: Your example would (and should) work perfectly in C++, as the bool values are really ints (C++ has overloads for BOOL and values like TRUE and FALSE - there is no native Boolean datatype).

But in C#, it's a bit "sloppy" to have the | operator be a bitwise OR in case and a logical OR in another, based on datatypes. C# was supposed to be more type safe but I consider the single pipe a "slip". Since C# doesn't define what int value a bool type has (0, 1, -1, etc.) internally, you shouldn't be able to use an operator that *normally* is used for bitwise operations (which only work on numbers, not abstract datatypes like a boolean). The fact that it does work is only a convenience in case you forget which to use (one pipe or two). Since most people would call this "sloppy" and choose to only use the two pipes, I thought it worth mentioning (since we're on the topic of "sloppy" coding in VB.NET versus the "clean" coding of C#).

-Nerseus
 
Back
Top