My Profile
Gifts
Active Members
TodayLast 7 Days
more...
|
Which language to choose C# or VB ?(Part2)
Posted Date: 05 Sep 2005 Resource Type: Articles Category: .NET Framework
|
Posted By: HimaBindu Veeramachaneni Member Level: Diamond Rating: Points: 10
|
After observing the MSIL code in both languages its clear that the VB IL code is more compared to C#. First, comparing the code size comments for each version, it’s clear that more MSIL code is involved with the VB version (//Code size 18 (0x12)) than with the C# version (//Code size 15 (0xf)). Second, the VB version expects and will use three stack slots (.maxstack 3), while the C# version expects and will use only two stack slots (.maxstack 2). From there on, all remains equal until we reach line IL_0008, at which point both versions drastically diverge. The C# version first pushes onto the stack the Boolean result of the call made to [mscorlib]System.String::op_Equality, which is the function C# uses to compare string values when using the == operator. Then, it immediately removes (pop) this same Boolean value from the stack right before returning execution to the caller. Although the last pop seems pointless, there’s really no choice in the matter because a method’s evaluation stack must be left empty before returning control to the caller, unless a value is to be returned, as is the case with non void methods. On the other hand, the VB version first pushes onto the stack the value 0 (ldc.i4.0), which will subsequently be popped off the stack as an argument to the call made to Microsoft.VisualBasic.CompilerServices.StringType::StrCmp, which is the function VB uses to compare string values when using the = operator, the Integer result of which is pushed onto the stack. Once that’s done, instruction ldc.i4.0 is once again used to push the value 0 onto the stack so that it may be subsequently compared to the Integer result of StrComp. Then, by means of instruction bne.un.s IL_0011, the last two values that were pushed onto the stack, that is, the results of the calls made to StrCmp and ldc.i4.0, are popped off, compared, and if inequality results, execution transfers to instruction IL_0011, which seems redundant since instruction IL_0011 follows immediately regardless of whether or not the result is inequality, yet most likely is used in order to pop off the stack the last two values that still remain on it. So the question becomes, why the discrepancy, regardless of how significant or insignificant it may be. Well, in this case, the answer is clear and simple. For backwards compatibility reasons, VB is forced to use StrCmp instead of String::op_Equality, the results of which are quite different, with the more drastic one being that StrCmp considers an empty string and a Null string equal, which is not the case with String::op_Equality, and, furthermore, this is most likely the main reason why the former is used instead of the latter. From a pure performance standpoint, String::op_Equality is superior to StrCmp, although the performance gains will manifest themselves only when an intense number of string comparisons are made, perhaps those made inside a tight loop.
Disadvantages of VB: 1. VB by default allows support for late binding. Although it can be turned off with “Option strict”, the culture is such that it’s usually left on. This leads to numerous difficult to catch errors. C# binding is always early. 2. VB still supports the old “On error goto” construct. This leads to sloppy or non-existent error handling. C# supports only the superior try…catch error handling. 3. VB supports optional parameters. Although VB developers often list this as an advantage, it is a disadvantage because the use of optional parameters weakens the contract between the caller and the method, allowing the developer to slacken his analysis and get away with it until bugs creep in. 4. VB supports the legacy VB functions, with reference to Microsoft.VisualBasic.dll. Many of these functions are slow and they should all be avoided in favor of the .NET framework. However many VB programmers still use them. In new VB projects, this dangerous namespace is included by default. 5. VB allows implementation of interfaces with methods of different names, making it confusing and difficult to find the implementation. C# does not. 6. VB supports background compilation. While this speeds the development cycle for small projects, it slows down the IDE in large projects, contributing at least in part to the culture tending to gravitate toward small projects. 7. C# namespaces are managed in way that makes programmers aware of namespaces and their importance. VB namespaces are managed in a way that hides them from the programmers by default. Careful attention to namespace management is a fundamental tenet of strong application design and its importance cannot be overestimated
8. Type casting is necessary in VB.NET
C# Advantages
• XML documentation generated from source code comments. (This is coming in VB.NET with Whidbey (the code name for the next version of Visual Studio and .NET), and there are tools which will do it with existing VB.NET code already.) • Operator overloading - again, coming to VB.NET in Whidbey. • Language support for unsigned types (you can use them from VB.NET, but they aren't in the language itself). Again, support for these is coming to VB.NET in Whidbey. • The using statement, which makes unmanaged resource disposal simple. • Explicit interface implementation, where an interface which is already implemented in a base class can be reimplemented separately in a derived class. Arguably this makes the class harder to understand, in the same way that member hiding normally does. • Unsafe code. This allows pointer arithmetic etc, and can improve performance in some situations. However, it is not to be used lightly, as a lot of the normal safety of C# is lost (as the name implies). Note that unsafe code is still managed code, i.e. it is compiled to IL, JITted, and run within the CLR And again We can’t say that Vb.Net is not preferred it has it’s own advantages over C#
VB.NET Advantages • Support for optional parameters - very handy for some COM interoperability • Support for late binding with Option Strict off - type safety at compile time goes out of the window, but legacy libraries which don't have strongly typed interfaces become easier to use. • Support for named indexers (ie. properties with parameters). • Various legacy VB functions (provided in the Microsoft.VisualBasic namespace, and can be used by other languages with a reference to the Microsoft.VisualBasic.dll). Many of these can be harmful to performance if used unwisely, however, and many people believe they should be avoided for the most part. • The with construct: it's a matter of debate as to whether this is an advantage or not, but it's certainly a difference. • Simpler (in expression - perhaps more complicated in understanding) event handling, where a method can declare that it handles an event, rather than the handler having to be set up in code. • The ability to implement interfaces with methods of different names. (Arguably this makes it harder to find the implementation of an interface, however.) • Catch ... When ... clauses, which allow exceptions to be filtered based on runtime expressions rather than just by type. • The VB.NET part of Visual Studio .NET compiles your code in the background. While this is considered an advantage for small projects, people creating very large projects have found that the IDE slows down considerably as the project gets larger.
Why C# Why not VB? C# is similar to C, C++ and Java hence increases readability.Many of the .NET features such as class declarations and object inheritance seemed more elegant in C# C# is a brand new language designed from the ground up for .NET. It's simple and not very verbose (less typing!) C# code executes faster than VB.Net code Hence Speed up the application’s performance C# allows you to define sections of "Unsafe" code, where you can use pointers and directly access memory. C# is the Microsoft preferred language because of its new and flexible feature. C# Handles Unsigned data types too . C# doesn't allow undeclared variables, untyped variables, and untyped methods like in VB. C# compilers exist for just about every platform imaginable, including Mac, Linux, Windows, Solaris, etc. There is not even ONE working VB.Net compiler on a non-Windows platform.
Summary Observed the advantages of C# over VB and performance wise C# is the best one hence we are preferring C#
|
Responses
|
| Author: critic 08 Sep 2005 | Member Level: Bronze Points : 0 | Please do not copy/paste articles from othersites. It is a serious violation of copyright laws.
| | Author: HimaBindu Veeramachaneni 17 Dec 2005 | Member Level: Diamond Points : 0 | Dear Critic, I have got a very good response to this article especially to part1 as many of our friends wrote me that they doesn't know the difference that the MSIL generated by VB and C# and their performance difference. Y many choose C# over VB? I just wanted to share the knowledge that I have with others inorder to technically grow myself too.
| | Author: sweetychinnu 05 May 2008 | Member Level: Gold Points : 2 | hi, if other's copy the article's u used to tell them that don't copy etc.... but y ur doing the same
This article and part (1) is copied from ... http://www.codeproject.com/KB/msil/vbvscsmsil.aspx
any how ...iam ur big fan..... i used to go through ur articles....
thanx but don't point out other's if ur also doing the same....
| | Author: Gaurav Arora 27 Jul 2008 | Member Level: Gold Points : 2 | Thanks Him! Its a nice article. Regrding copy/paste, its my persnal experience that knowledge is free. Yes, I agree to copy and use the whole contents of anyone.
If anyone inherited someone's reading/article and present it with more wonderful way then I think this is not a violation.
Anyway, its a matter of debate, but I am very much inpired from your writting. I am also trying to do the same. Could you please let me know more details to get involved in these types of articles. My email id is g_arora@hotmail.com
Thanks in advance. Gaurav Arora
|
|
|