帝国软件
  设为首页 加入收藏 关于我们
 
解密帝国网站管理系统
栏 目:
 
您的位置:首页 > 技术文档 > ASP编程
The Battle: C#  VS. Visual Basic
作者:Don 发布时间:2005-03-12 来源:www.microsoft.com
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!


  
评论】【加入收藏夹】【 】【打印】【关闭
※ 相关链接
 ·ASP.NET Authentication Provide  (2005-03-12)

   栏目导行
  PHP编程
  ASP编程
  ASP.NET编程
  JAVA编程
   站点最新
·致合作伙伴的欢迎信
·媒体报道
·帝国软件合作伙伴计划协议
·DiscuzX2.5会员整合通行证发布
·帝国CMS 7.0版本功能建议收集
·帝国网站管理系统2012年授权购买说
·PHPWind8.7会员整合通行证发布
·[官方插件]帝国CMS-访问统计插件
·[官方插件]帝国CMS-sitemap插件
·[官方插件]帝国CMS内容页评论AJAX分
   类别最新
·在ASP中使用数据库
·使用ASP脚本技术
·通过启动脚本来感受ASP的力量
·学习使用ASP对象和组件
·解析asp的脚本语言
·初看ASP-针对初学者
·ASP开发10条经验总结
·ASP之对象总结
·ASP与数据库应用(给初学者)
·关于学习ASP和编程的28个观点
 
关于帝国 | 广告服务 | 联系我们 | 程序开发 | 网站地图 | 留言板 帝国网站管理系统