直播中
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!