473,395 Members | 1,535 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

VB.NET and C# Comparison

I've created a quick reference comparison of C# and
VB.NET that you might find useful. I couldn't find
anything like it on the web and so I made my own.

http://www.harding.edu/USER/fmccown/...t_csharp_compa
rison.html

It doesn't cover every area of the languages, just those
features most frequently used.

I'd be thankful for any suggestions you may have on ways
to improve the comparisons.

Thanks,
Frank
Nov 20 '05 #1
10 1595
In article <04****************************@phx.gbl>, Frank McCown wrote:
I've created a quick reference comparison of C# and
VB.NET that you might find useful. I couldn't find
anything like it on the web and so I made my own.

http://www.harding.edu/USER/fmccown/...t_csharp_compa
rison.html

It doesn't cover every area of the languages, just those
features most frequently used.

I'd be thankful for any suggestions you may have on ways
to improve the comparisons.

Thanks,
Frank


I'd like to point out Frank that you have a technical inaccuracy on your
enum section. The word stop can be used as an enum value. You just
have to do it like this:

Enum Action
Start
[Stop]
Rewind
Forward
End Enum

Dim a As Action = Action.Stop

Also, further down in the For Each statement. In 1.1, you can declare
the loop variable inside the For Each statement:

For Each s As String In names
...
Next

That also applies to the For Loop

For i As Integer = 0 To nums.Length
...
Next

Another point (sorry, pointing these out as I go):

VB.NET's try catch block can have conditions (when clause):

Dim x As Integer = 10
Dim y As Integer = 0
Dim z As Integer

Try
z = x / y
Catch ex As Exception When y = 0
....
Finally
....
End Try

you can't do that in C# :)

In the properties - you have foo.Size++ in the VB.NET side. That should
be foo.Size += 1

On the events - you might want to point out that RaiseEvent will not
throw an exception if the event is a null reference. In c# you have to
check that before you invoke the delegate.

On console, IO - on the C# side. You don't have to use the
Convert.ToChar method to convert an int to a char. You can just cast
it:

Console.WriteLine("{0}", (char) 65);
Other then those, it looks pretty good. I hope you don't take this post
the wrong way, just pointing out a few little things.

--
Tom Shelton
MVP [Visual Basic]
Nov 20 '05 #2
Thanks Tom

Catch ex As Exception When y = 0
I never knew that one.

Hi Frank,

Like Tom I think your work is highly worthy and I'm also am just applying
my polishing rag. I'm assuming that was what you were hoping for anyway, as
well as giving <us> a useful resource. It will come in handy when we get the
'I have some C# snippets can you...' queries.

So - DreamWeaver at the ready?
In VB you can have IIf instead of C#'s ()?:
It's common but I consider it poor practice to explicitly assign true or
false when the condition itself gives the value
teen = (age >= 13 And age <= 19)

I would use something more like:
greeting = IIf (age >= 13 And age <= 19, "Uugh!", "Hello")
The line continuation can be used to good effect to split an If but omit
the End If

If SomethingOrOtherRatherLongWindedToDefine Then _
DoSomethingConsequential (AndWith, Lots, OfArgs)
With ' Resize the array, keeping the existing values (optional)
I'd have both versions separately so that there's no ambiguity about
what's optional.
In 'Pass by value ... you might want to note that ByVal is the default and
can be omitted.

Dim a = 1, b = 1, c As Integer
Console.WriteLine("{0} {1} {2}", a, b, c) ' 1 2 5

b should be 2 for your output but c - initialised by default will be
zero - giving '1 2 0
Again, with the ByVal - ByVal ParamArray can be just ParamArray.
With /* C# doesn't support optional arguments/parameters, I'd actually
show the overload explicitly (and introduce that word)
'Depricated' is spelt with an 'e'. Lol. When I looked it up in the
dictionary I used an 'a' and the dictionary put <me> right, too. ;-)
Given that your audience is students, they may appreciate this example:
Dim Up As New Exception ("Uuurgh - can't handle this.")
Throw Up
Interface IAlarmClock
Inherits ISuperAlarm

These should be reversed I believe. Unfortunately that mean that Batman
inherits from something Super, but SuperAlarm inherits from something
ordinary. This is correct, codingwise, but conceptually liable to mislead
(small risk but present). I'd lose one of the 'Super's. I like the Batman one,
so - Interface IAlarmClock : Inherits IClock. This is one of the few places
that I use the : syntax to put the two lines together.
Classes can have constructors:
Public Shared Sub New

You might also like to point out Private constructors for Shared-only
classes.
.Name = "SpamMan"
.PowerLevel = 3
SpamMan, like VirusMan and WormMan, is a force for evil and therefore has
negative powers. ;-)
hero.Rest() ' Calling Shared method
' or
SuperHero.Rest()

SuperHero is the Shared method.
Dim hero2 As SuperHero = hero ' Makes shallow copy
Copy is very misleading especially when used with 'shallow' as this latter
word is properly used with 'clone'. The assignment is duplicated reference but
not a copy of the object (as you show in the following code).

You might, therefore, want to introduce MemberwiseClone (shallow) and
Clone (deep) in contrast to the duplicated reference. All objects have
MemberWiseClone, Clone needs to be explicitly implemented.
Dim stu2 As StudentRecord = stu ' Makes deep copy
This one makes a shallow copy. A deep copy is where any referenced objects
are also copied - and perhaps (if implemented that way) references to their
ref... etc.
In Character constants, you might like to demonstrate embedded double
quotes.
Gossip = "So she said ""with a kipper"". No joking - that's what she
said..."

There's also:
Dim InitialLetter As Char = "F"c
If InitialLetter >= "A"c And InitialLetter <= "Z"c Then ...
You've got
age = Convert.ToInt32(Console.ReadLine())
There's still
age = Val (Console.ReadLine())
which is much more forgiving, especially for unchecked console input.
Phew. I bit off more than I thought there!!

I'd suggest posting this to these groups as well. They must get lots of
conversion queries too.
languages.csharp
dotnet.general
dotnet.academic

Regards,
Fergus
Nov 20 '05 #3
Thank you Tom and Fergus for your excellent comments.
I'll make some corrections very soon.

Frank
Nov 20 '05 #4
> Dim stu2 As StudentRecord = stu ' Makes deep copy
This one makes a shallow copy. A deep copy is where any referenced objectsare also copied - and perhaps (if implemented that way) references to theirref... etc.
Regards,
Fergus


I REALLY appreciate the input. I do have a question
about copying structures though... I read threw the MSDN
VB.NET documentation on structures and saw this:

"When you assign one structure variable to another... the
current values of all the variable members are copied to
the new structure."

So isn't the assigning of a structure to another actually
performing a deep copy?

Thanks,
Frank
Nov 20 '05 #5
Hi Frank,

Structure assignments are shallow copies, ie. only the ValueType fields
and strings. Strings - despite being a reference type - are a special case as
they are immutable, meaning that string data can only be pointed to by a
single variable/field/whatever at a time.

The documentation is correct. The 'value' of an object reference is the
address of the object not the object itself. So only the address gets copied.

So, any objects <referenced> by the structure (this includes arrays) will
not be copied (but the reference will). Nested structures, however, being
ValueTypes, <will> be copied (though not, of course, <their> referenced
objects)..

Here's an example:

Structure Nest
Public oMother As clsBird 'Reference copied
Public aoEggs() As structEgg 'Reference Copied
Public oCuckooEgg As structEgg 'Copied
Public dCreated As DateTime 'Copied
Public iHoursTaken As Integer 'Copied
Public sMaterials As String 'Copied
End Structure

I briefly mentioned 'shallow' versus 'deep' in the other posting so it'll
make more sense when you find that bit again. But here's some more about it.

All the above was about 'shallow' copying - valuetypes, references but not
actual objects. 'Deep' copying therefore goes beyond the object in question
(whether structure or class) and copies the linked objects too. 'Deep copying'
is a phrase associated with Clone and Copy methods. Each class that implements
these has a choice of whether to do it as a shallow copy or a deep copy. If
shallow is chosen, then MemberwiseClone can be returned. If it's a deep copy
though, the referenced objects must be asked to clone themselves as well. Each
of these, in turn, has the same choice and may respond with a shallow or deep
copy of itself.

In the following conversation Jay talks more about Cloning.
http://tinyurl.com/ta40

Regards,
Fergus
Nov 20 '05 #6
Fergus,
Structure assignments are shallow copies, ie. only the ValueType fields and strings. Strings - despite being a reference type - are a special case as they are immutable, meaning that string data can only be pointed to by a
single variable/field/whatever at a time. Is this what you wanted to say?

Strings are immutable in that you cannot change their contents, Strings are
not special in this regard per se as you are free to make any other class or
structure immutable.

Remember Immutable means not changeable.
meaning that string data can only be pointed to by a
single variable/field/whatever at a time.
That's not correct, any number of string variables can point to the same
instance of a string object (aka "string data")!

Dim s1 As String = "Hello world"
Dim s2 As String = s1

There is only one string object in the above sample, both variables are
referencing it.

Hope this helps
Jay

"Fergus Cooney" <fi****@post.com> wrote in message
news:%2******************@TK2MSFTNGP11.phx.gbl... Hi Frank,

Structure assignments are shallow copies, ie. only the ValueType fields and strings. Strings - despite being a reference type - are a special case as they are immutable, meaning that string data can only be pointed to by a
single variable/field/whatever at a time.

The documentation is correct. The 'value' of an object reference is the address of the object not the object itself. So only the address gets copied.
So, any objects <referenced> by the structure (this includes arrays) will not be copied (but the reference will). Nested structures, however, being
ValueTypes, <will> be copied (though not, of course, <their> referenced
objects)..

Here's an example:

Structure Nest
Public oMother As clsBird 'Reference copied
Public aoEggs() As structEgg 'Reference Copied
Public oCuckooEgg As structEgg 'Copied
Public dCreated As DateTime 'Copied
Public iHoursTaken As Integer 'Copied
Public sMaterials As String 'Copied
End Structure

I briefly mentioned 'shallow' versus 'deep' in the other posting so it'll make more sense when you find that bit again. But here's some more about it.
All the above was about 'shallow' copying - valuetypes, references but not actual objects. 'Deep' copying therefore goes beyond the object in question (whether structure or class) and copies the linked objects too. 'Deep copying' is a phrase associated with Clone and Copy methods. Each class that implements these has a choice of whether to do it as a shallow copy or a deep copy. If shallow is chosen, then MemberwiseClone can be returned. If it's a deep copy though, the referenced objects must be asked to clone themselves as well. Each of these, in turn, has the same choice and may respond with a shallow or deep copy of itself.

In the following conversation Jay talks more about Cloning.
http://tinyurl.com/ta40

Regards,
Fergus

Nov 20 '05 #7
Hi Jay,

I wanted to get a new slant on 'immutable' but I chose an incorrect
'slant'. And, having picked up that ball, I succesfully ran down the field
with it to score an own-goal. Oops! ;-)

I suppose I could have qualified it by saying

|| meaning that string data can
effectively
|| only be pointed to by a
|| single whatever at a time

in the sense that as soon as you do anything to a string, separation
occurs.

But that runs the risk, as you pointed out, of being misleading on the
term 'immutable'

Thanks, Jay, a useful correction.

Cheers,
Fergus
Nov 20 '05 #8
"Fergus Cooney" <fi****@post.com> wrote...

Immutable strings are passe'. I think the language needs uni-mutable (these
could be "muted" once), semi-mutable (you can change part of the string but
not the entire string and de-mutable (these could revert to some previous
version of the string.) Frankly the one that's missing is auto-mutable.
These just change periodically on their own.


Nov 20 '05 #9
Hi Tom,

ROFL - clever, too. :-))

Regards,
Fergus
Nov 20 '05 #10
Hi Tom,

|| Frankly the one that's missing is auto-mutable.
|| These just change periodically on their own

Apparently this has already been implemented for Bound TextBoxes on a
Tabbed Page. No announcement has been made, as people will be demanding it in
other contexts, but the silent roll-out has caused misunderstanding, with some
wondering "why this no work".

Regards,
Fergus
Nov 20 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: bettervssremoting | last post by:
To view the full article, please visit http://www.BetterVssRemoting.com Better VSS Remote Access Tool This article makes a detailed comparison among SourceAnyWhere, SourceOffSite, VSS...
29
by: Steven D'Aprano | last post by:
Playing around with comparisons of functions (don't ask), I discovered an interesting bit of unintuitive behaviour: >>> (lambda y: y) < (lambda y: y) False Do the comparison again and things...
5
by: mayamorning123 | last post by:
A comparison among six VSS remote tools including SourceOffSite , SourceAnyWhere, VSS Connect, SourceXT, VSS Remoting, VSS.NET To view the full article, please visit...
46
by: yadurajj | last post by:
Hello i am newbie trying to learn C..I need to know about string comparisons in C, without using a library function,...recently I was asked this in an interview..I can write a small program but I...
0
by: bettervssremoting | last post by:
To view the full article, please visit http://www.BetterVssRemoting.com Better VSS Remote Access Tool This article makes a detailed comparison among SourceAnyWhere, SourceOffSite, VSS...
37
by: spam.noam | last post by:
Hello, Guido has decided, in python-dev, that in Py3K the id-based order comparisons will be dropped. This means that, for example, "{} < " will raise a TypeError instead of the current...
7
by: bcutting | last post by:
I am looking for a way to take a large number of images and find matches among them. These images may not be exact replicas. Images may have been resized, cropped, faded, color corrected, etc. ...
0
by: SvenMathijssen | last post by:
Hi, I've been wrestling with a problem for some time that ought to be fairly simple, but turns out to be very difficult for me to solve. Maybe someone here knows the answer. What I try to do is...
1
by: Lars B | last post by:
Hey guys, I have written a C++ program that passes data from a file to an FPGA board and back again using software and DMA buffers. In my program I need to compare the size of a given file against...
11
by: Andrus | last post by:
I created dynamic extension methods for <= and < SQL comparison operators: public static IQueryable<TLessThanOrEqual<T>(this IQueryable<Tsource, string property, object value); public static...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.