设为首页
联系我们
加入收藏
| 网络工具 | 系统工具 | 应用软件 | 多媒体类 | 联络聊天 | 行业软件 | 图形图像 | 安全相关 | 编程开发 | 教育教学 | 游戏娱乐 |
| 程序源码 | ASP 源码 | CGI 源码 | PHP 源码 | 驱动下载 | 字体下载 | 素材下载 | 桌面大全 | 闪客天地 | 外挂插件 | 考题下载 |

站内搜索:

您的位置:首页-> 资讯中心-> 技术开发-> ASP 学院-> ASP文摘-> The Battle: C# VS. Visual Basic

ASP文摘

ASP教程
ASP文摘
ASP实例
ASP FAQ
ASP安全
ASP组件
ASP其它

本类阅读TOP10

·负载平衡环境下的ASP会话管理(转)(二)
·Chinaasp论坛精华帖全文检索器技术内幕
·如何解决图形和文字的显示问题
·ASP中处理#include
·Microsoft Script Debugger说明书
·在ASP处理程序时,进度显示
·全面考察“禁用浏览器后退按钮”
·ASP中FSO的神奇功能 - 权限许可
·用ASP创建Microsoft Word 文件
·关于数据库连接的一点浅见

精品推荐

The Battle: C# VS. Visual Basic

作者:未知 来源:未知 加入时间:2004-12-1

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!

(出处:不详 )





相关文章
相关软件