当前位置:首页 > 网络编程 > WEB编程 > ASP.net > The Battle: C#  VS. Visual Basic

The Battle: C#  VS. Visual Basic

点击次数:17 次 发布日期:2008-11-27 00:13:58 作者:源代码网
源代码网推荐 There has been many questions of which language is better to use with .NET, some say C# some say VB, and some say managed C. So we decided to give you some more detail and let you make up your mind yourself. I think you"ll find that the best language to use, is the one you want to use.

The first thing everyone should get familiar with is what managed code or IL language is.

When developing for .NET one can use any number of languages COBOL, Visual Basic, C, and C#. The question is what is best? Well personally I use C#. I was traditionally a Visual Basic programmer and found learning C# very easy. Not only that, but in most cases I write less code than I would using Visual Basic to do the same thing.

So why is Microsoft stating that there should be no difference in speed at runtime? "How can they say?" that the C programmers are thinking. Well let"s use the IL Dissembler [IL DASM][ILdasm.exe] to demonstrate:

** You should be able to find the IL DASM under tools in the NGWS SDK.


源代码网推荐We are going to be using two different namespaces and classes to demonstrate. Below are contents of both the Visual Basic Namespace and the C# NameSpace. The classes are exactly the same, except they are written in different languages.


源代码网推荐C#:

using System.Data;
源代码网推荐using System.Data.SQL;
源代码网推荐namespace ILTest
源代码网推荐{
源代码网推荐 public class ILTestCS
源代码网推荐 {
源代码网推荐 public DataView getAuthors()
源代码网推荐 {
源代码网推荐 SQLConnection sConn;
源代码网推荐 sConn = new SQLConnection(
源代码网推荐 "SERVER=LOCALHOST;UID=aspCasulity;DataBase=PUBS;PWD=asp");
源代码网推荐 SQLDataSetCommand sCmd;
源代码网推荐 sCmd = new SQLDataSetCommand("SELECT * FROM Authors",sConn);
源代码网推荐 DataSet ds;
源代码网推荐 ds = new DataSet();
源代码网推荐 sCmd.FillDataSet(ds,"MyTable");
源代码网推荐 return ds.Tables[0].DefaultView;
源代码网推荐 }
源代码网推荐 }
源代码网推荐}


源代码网推荐Visual Basic:

Imports System.Data
源代码网推荐Imports System.Data.SQL
源代码网推荐Namespace ILTestvb
源代码网推荐 Public Class ILTestVB
源代码网推荐 Public Function getAuthors() As DataView
源代码网推荐 Dim sConn As SQLConnection
源代码网推荐 sConn = New SQLConnection("SERVER=LOCALHOST;UID=aspCasulity;DataBase=PUBS;PWD=asp")
源代码网推荐 Dim sCmd As SQLDataSetCommand
源代码网推荐 sCmd = New SQLDataSetCommand("SELECT * FROM Authors", sConn)
源代码网推荐 Dim ds As DataSet
源代码网推荐 ds = New DataSet
源代码网推荐 sCmd.FillDataSet(ds, "MyTable")
源代码网推荐 Return ds.Tables(0).DefaultView
源代码网推荐 End Function
源代码网推荐 End Class
源代码网推荐End Namespace


源代码网推荐After we build the two dll"s we can open them up in ILDASM. To open up the IL code you simply launch the ildasm.exe, click open and find the dll. We are going to be looking at the getAuthors Method. Notice anything? They are nearly identical.

See below for IL code:

C#

.method public hidebysig instance class ["System.Data"]System.Data.DataView
源代码网推荐 getProducts() il managed
源代码网推荐{
源代码网推荐// Code size 64 (0x40)
源代码网推荐 .maxstack 3
源代码网推荐 .locals ([0] class ["System.Data"]System.Data.SQL.SQLConnection sConn,
源代码网推荐 [1] class ["System.Data"]System.Data.SQL.SQLDataSetCommand sCmd,
源代码网推荐 [2] class ["System.Data"]System.Data.DataSet ds,
源代码网推荐 [3] class ["System.Data"]System.Data.DataView V_3)
源代码网推荐 IL_0000: ldstr "SERVER=LOCALHOST;UID=aspCasulity;DataBase=PUBS;PWD=asp"
源代码网推荐 IL_0005: newobj instance void ["System.Data"]System.Data.SQL.SQLConnection::.ctor(class System.String)
源代码网推荐 IL_000a: stloc.0
源代码网推荐 IL_000b: ldstr "SELECT * FROM Authors"
源代码网推荐 IL_0010: ldloc.0
源代码网推荐 IL_0011: newobj instance void ["System.Data"]System.Data.SQL.SQLDataSetCommand::.ctor(class System.String,
源代码网推荐 class ["System.Data"]System.Data.SQL.SQLConnection)
源代码网推荐 IL_0016: stloc.1
源代码网推荐 IL_0017: newobj instance void ["System.Data"]System.Data.DataSet::.ctor()
源代码网推荐 IL_001c: stloc.2
源代码网推荐 IL_001d: ldloc.1
源代码网推荐 IL_001e: ldloc.2
源代码网推荐 IL_001f: ldstr "MyTable"
源代码网推荐 IL_0024: callvirt instance int32 ["System.Data"]System.Data.Internal.DBDataSetCommand::FillDataSet(class ["System.Data"]System.Data.DataSet,
源代码网推荐 class System.String)
源代码网推荐 IL_0029: pop
源代码网推荐 IL_002a: ldloc.2
源代码网推荐 IL_002b: call instance class ["System.Data"]System.Data.TablesCollection ["System.Data"]System.Data.DataSet::get_Tables()
源代码网推荐 IL_0030: ldc.i4.0 IL_0031: call instance class ["System.Data"]System.Data.DataTable ["System.Data"]System.Data.TablesCollection::get_Item(int32)
源代码网推荐 IL_0036: call instance class ["System.Data"]System.Data.DataView ["System.Data"]System.Data.DataTable::get_DefaultView()
源代码网推荐 IL_003b: stloc.3
源代码网推荐 IL_003c: br.s IL_003e
源代码网推荐 IL_003e: ldloc.3
源代码网推荐 IL_003f: ret
源代码网推荐} // end of method "ILTestCS::getAuthors"


源代码网推荐Visual Basic

.method public instance class ["System.Data"]System.Data.DataView
源代码网推荐 getProducts() il managed forwardref
源代码网推荐{
源代码网推荐 // Code size 67 (0x43)
源代码网推荐 .maxstack 3
源代码网推荐 .locals init ([0] class ["System.Data"]System.Data.DataSet ds,
源代码网推荐 [1] class ["System.Data"]System.Data.DataView getProducts,
源代码网推荐 [2] class ["System.Data"]System.Data.SQL.SQLDataSetCommand sCmd,
源代码网推荐 [3] class ["System.Data"]System.Data.SQL.SQLConnection sConn)
源代码网推荐 IL_0000: nop
源代码网推荐 IL_0001: ldstr "SERVER=LOCALHOST;UID=aspCasulity;DataBase=PUBS;PWD=asp"
源代码网推荐 IL_0006: newobj instance void ["System.Data"]System.Data.SQL.SQLConnection::.ctor(class System.String)
源代码网推荐 IL_000b: stloc.3
源代码网推荐 IL_000c: ldstr "SELECT * FROM Authors"
源代码网推荐 IL_0011: ldloc.3
源代码网推荐 IL_0012: newobj instance void ["System.Data"]System.Data.SQL.SQLDataSetCommand::.ctor(class System.String,
源代码网推荐 class ["System.Data"]System.Data.SQL.SQLConnection)
源代码网推荐 IL_0017: stloc.2
源代码网推荐 IL_0018: newobj instance void ["System.Data"]System.Data.DataSet::.ctor()
源代码网推荐 IL_001d: stloc.0
源代码网推荐 IL_001e: ldloc.2
源代码网推荐 IL_001f: ldloc.0
源代码网推荐 IL_0020: ldstr "MyTable"
源代码网推荐 IL_0025: callvirt instance int32 ["System.Data"]System.Data.Internal.DBDataSetCommand::FillDataSet(class ["System.Data"]System.Data.DataSet,
源代码网推荐 class System.String)
源代码网推荐 IL_002a: pop
源代码网推荐 IL_002b: ldloc.0
源代码网推荐 IL_002c: call instance class ["System.Data"]System.Data.TablesCollection ["System.Data"]System.Data.DataSet::get_Tables()
源代码网推荐 IL_0031: ldc.i4.0
源代码网推荐 IL_0032: call instance class ["System.Data"]System.Data.DataTable ["System.Data"]System.Data.TablesCollection::get_Item(int32)
源代码网推荐 IL_0037: call instance class ["System.Data"]System.Data.DataView ["System.Data"]System.Data.DataTable::get_DefaultView()
源代码网推荐 IL_003c: stloc.1
源代码网推荐 IL_003d: nop
源代码网推荐 IL_003e: br.s IL_0040
源代码网推荐 IL_0040: ldloc.1
源代码网推荐 IL_0041: nop
源代码网推荐 IL_0042: ret
源代码网推荐} // end of method "ILTestVB::getAuthors"


源代码网推荐As you can see the IL code generated by C# and Visual Basic are quite similar. Microsoft has stated that in some situations IL code generated in VB would execute faster than code generated in another .NET compilable language and vice versa, but overall you should not see much of a difference. So as far as I can see, the best language to use when developing .NET solutions is, the one you like the best.

I hope that this anwsers some questions for all of you!


源代码网推荐
源代码网供稿.

网友评论 (0)
会员中心
网络编程
本站推荐
网络编程之精华