VB.NET和C#比较 [英] VB.NET and C# Comparison

查看:89
本文介绍了VB.NET和C#比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个C#和

VB.NET的快速参考比较,您可能会发现它很有用。我在网上找不到任何类似的东西,所以我自己制作了它。

http://www.harding.edu/USER/fmccown/...t_csharp_compa

rison .html


它并不涵盖语言的每个区域,只是那些最常用的功能。


如果您有任何改进比较的建议,我将感激不尽。


谢谢,

Frank

I''ve created a quick reference comparison of C# and
VB.NET that you might find useful. I couldn''t find
anything like it on the web and so I made my own.

http://www.harding.edu/USER/fmccown/...t_csharp_compa
rison.html

It doesn''t cover every area of the languages, just those
features most frequently used.

I''d be thankful for any suggestions you may have on ways
to improve the comparisons.

Thanks,
Frank

推荐答案

文章< 04 ********************* *******@phx.gbl> ;, Frank McCown写道:
In article <04****************************@phx.gbl>, Frank McCown wrote:
我已经创建了一个你可能觉得有用的C#和
VB.NET的快速参考比较。我无法在网上找到类似的东西,所以我自己做了。

http://www.harding.edu/USER/fmccown/...t_csharp_compa
rison.html

我会感谢您对方式的任何建议。 >改进比较。

谢谢,
Frank
I''ve created a quick reference comparison of C# and
VB.NET that you might find useful. I couldn''t find
anything like it on the web and so I made my own.

http://www.harding.edu/USER/fmccown/...t_csharp_compa
rison.html

It doesn''t cover every area of the languages, just those
features most frequently used.

I''d be thankful for any suggestions you may have on ways
to improve the comparisons.

Thanks,
Frank




我想指出弗兰克你有一个你的

enum部分的技术不准确。单词stop可以用作枚举值。你只需要这样做:
必须这样做:


枚举行动

开始

[停止]

后退

前进

结束枚举


Dim a As Action = Action.Stop


此外,在For Each声明中进一步说明。在1.1中,你可以在For Each语句中声明

循环变量:


For each s As String in Name

...

下一页


这也适用于For循环


For i As Integer = 0 To nums.Length

...

下一页


另一点(抱歉,我指着这些指出):


VB.NET'的try catch块可以有条件(when子句):


Dim x As Integer = 10

Dim y As Integer = 0

Dim z As Integer


尝试

z = x / y

Catch ex As Exception当y = 0

....

最后

....

结束尝试


你不能在C#中做到这一点。


在属性中 - 你有foo.Size ++ in VB.NET方面。这应该是

是foo.Size + = 1


关于事件 - 你可能想指出RaiseEvent不会

如果事件是空引用,则抛出异常。在c#中你必须在调用委托之前检查它。


在控制台上,IO - 在C#端。您不必使用

Convert.ToChar方法将int转换为char。你可以施展

吧:


Console.WriteLine(" {0}",(char)65);

除此之外,它看起来还不错。我希望你不要拿这个帖子

错误的方式,只是指出一些小东西。


-

Tom Shelton

MVP [Visual Basic]



I''d like to point out Frank that you have a technical inaccuracy on your
enum section. The word stop can be used as an enum value. You just
have to do it like this:

Enum Action
Start
[Stop]
Rewind
Forward
End Enum

Dim a As Action = Action.Stop

Also, further down in the For Each statement. In 1.1, you can declare
the loop variable inside the For Each statement:

For Each s As String In names
...
Next

That also applies to the For Loop

For i As Integer = 0 To nums.Length
...
Next

Another point (sorry, pointing these out as I go):

VB.NET''s try catch block can have conditions (when clause):

Dim x As Integer = 10
Dim y As Integer = 0
Dim z As Integer

Try
z = x / y
Catch ex As Exception When y = 0
....
Finally
....
End Try

you can''t do that in C# :)

In the properties - you have foo.Size++ in the VB.NET side. That should
be foo.Size += 1

On the events - you might want to point out that RaiseEvent will not
throw an exception if the event is a null reference. In c# you have to
check that before you invoke the delegate.

On console, IO - on the C# side. You don''t have to use the
Convert.ToChar method to convert an int to a char. You can just cast
it:

Console.WriteLine("{0}", (char) 65);
Other then those, it looks pretty good. I hope you don''t take this post
the wrong way, just pointing out a few little things.

--
Tom Shelton
MVP [Visual Basic]


谢谢Tom


Catch ex As Exception当y = 0

我从来不知道那个。


嗨弗兰克,


和汤姆一样我认为你的工作是非常有价值的,我也正在申请我的抛光抹布

。我假设这是你所希望的,因为

以及给予< us>一个有用的资源。当我们得到

''我有一些C#片段可以......''查询时它会派上用场。


所以 - DreamWeaver在准备好了吗?

在VB中,您可以使用IIf而不是C#'()?:

这很常见,但我认为明确指定true或

假如条件本身给出的值

teen =(age> = 13且年龄<= 19)


我会使用更像的东西:

greeting = IIf(年龄> = 13年龄< = 19,Uugh!,你好)

行继续可以用来很好地分割一个If但是省略

结束如果


如果SomethingOrOtherRatherLongWindedToDefine那么_

DoSomethingConsequential(AndWith,Lots,OfArgs)

使用''调整数组大小,保持现有值(可选)

我将两个版本分开,以便关于

什么是可选的,没有任何含糊之处/>
在''按值传递......你可能需要注意ByVal是默认值,

可以省略。


Dim a = 1,b = 1,c As Integer

Console.WriteLine(" {0} {1} {2}",a,b,c)''1 2 5


b应该是2为你的输出,但c - 默认初始化将是

零 - 给''1 2 0

再次,使用ByVal - ByVal ParamArray可以只是ParamArray。

使用/ * C#不支持可选的参数/参数,我实际上会给b
$ b显示超载明确地(并引入那个词)

''Depricated''拼写为''e''。大声笑。当我在

词典中查找时,我使用了''a''字典和< me>对,也是。 ;-)

鉴于您的观众是学生,他们可能会欣赏这个例子:

作为新例外情况变暗(Uuurgh - 无法解决这个问题。 ;)

扔掉

接口IAlarmClock

继承ISuperAlarm


这些应该颠倒我相信。不幸的是,这意味着蝙蝠侠

继承了Super的东西,但SuperAlarm继承了普通的
。这是正确的,编码方式,但在概念上容易误导

(小风险但存在)。我会失去一个''超级'。我喜欢蝙蝠侠,

所以 - 接口IAlarmClock:继承IClock。这是我用的几个地方之一

:将两行放在一起的语法。

类可以有构造函数:

Public共享子新


您可能还想指出仅限共享的私有构造函数

类。

.Name =" ; SpamMan"

.PowerLevel = 3

SpamMan,像VirusMan和WormMan一样,是邪恶的力量,因此具有

的负面力量。 ;-)

hero.Rest()''调用共享方法

''或

SuperHero.Rest()


SuperHero是共享方法。

昏暗的英雄2作为SuperHero =英雄''使浅份副本

复制是非常误导的,特别是与''一起使用时浅''后者

字与''克隆'正确使用。赋值是重复引用,但

不是对象的副本(如下面的代码所示)。


因此,您可能想要介绍与重复的引用相比,MemberwiseClone(浅)和

克隆(深)。所有对象都有

MemberWiseClone,Clone需要明确实现。

Dim stu2作为StudentRecord = stu''做深拷贝

这个让浅拷贝。深度复制是指任何引用的对象

也被复制 - 并且可能(如果以这种方式实现)引用它们的

ref ...等等。

在字符常量中,您可能希望演示嵌入的双

引号。

Gossip ="所以她说用鲱鱼。不开玩笑 - 这就是她所说的...... b $ b说... ...


还有:

昏暗InitialLetter As Char =" F" c

如果InitialLetter> =" A" c和InitialLetter< =" Z" c那么......

你'已经有了

age = Convert.ToInt32(Console.ReadLine())

还有

age = Val(控制台) .ReadLine())

更宽容,特别是对于未经检查的控制台输入。

Phew。我比我想象的要多得多!!


我建议也将这些发布给这些团体。他们也必须获得大量的

转换查询。

languages.csharp

dotnet.general

dotnet.academic


问候,

Fergus
Thanks Tom

Catch ex As Exception When y = 0
I never knew that one.

Hi Frank,

Like Tom I think your work is highly worthy and I''m also am just applying
my polishing rag. I''m assuming that was what you were hoping for anyway, as
well as giving <us> a useful resource. It will come in handy when we get the
''I have some C# snippets can you...'' queries.

So - DreamWeaver at the ready?
In VB you can have IIf instead of C#''s ()?:
It''s common but I consider it poor practice to explicitly assign true or
false when the condition itself gives the value
teen = (age >= 13 And age <= 19)

I would use something more like:
greeting = IIf (age >= 13 And age <= 19, "Uugh!", "Hello")
The line continuation can be used to good effect to split an If but omit
the End If

If SomethingOrOtherRatherLongWindedToDefine Then _
DoSomethingConsequential (AndWith, Lots, OfArgs)
With '' Resize the array, keeping the existing values (optional)
I''d have both versions separately so that there''s no ambiguity about
what''s optional.
In ''Pass by value ... you might want to note that ByVal is the default and
can be omitted.

Dim a = 1, b = 1, c As Integer
Console.WriteLine("{0} {1} {2}", a, b, c) '' 1 2 5

b should be 2 for your output but c - initialised by default will be
zero - giving ''1 2 0
Again, with the ByVal - ByVal ParamArray can be just ParamArray.
With /* C# doesn''t support optional arguments/parameters, I''d actually
show the overload explicitly (and introduce that word)
''Depricated'' is spelt with an ''e''. Lol. When I looked it up in the
dictionary I used an ''a'' and the dictionary put <me> right, too. ;-)
Given that your audience is students, they may appreciate this example:
Dim Up As New Exception ("Uuurgh - can''t handle this.")
Throw Up
Interface IAlarmClock
Inherits ISuperAlarm

These should be reversed I believe. Unfortunately that mean that Batman
inherits from something Super, but SuperAlarm inherits from something
ordinary. This is correct, codingwise, but conceptually liable to mislead
(small risk but present). I''d lose one of the ''Super''s. I like the Batman one,
so - Interface IAlarmClock : Inherits IClock. This is one of the few places
that I use the : syntax to put the two lines together.
Classes can have constructors:
Public Shared Sub New

You might also like to point out Private constructors for Shared-only
classes.
.Name = "SpamMan"
.PowerLevel = 3
SpamMan, like VirusMan and WormMan, is a force for evil and therefore has
negative powers. ;-)
hero.Rest() '' Calling Shared method
'' or
SuperHero.Rest()

SuperHero is the Shared method.
Dim hero2 As SuperHero = hero '' Makes shallow copy
Copy is very misleading especially when used with ''shallow'' as this latter
word is properly used with ''clone''. The assignment is duplicated reference but
not a copy of the object (as you show in the following code).

You might, therefore, want to introduce MemberwiseClone (shallow) and
Clone (deep) in contrast to the duplicated reference. All objects have
MemberWiseClone, Clone needs to be explicitly implemented.
Dim stu2 As StudentRecord = stu '' Makes deep copy
This one makes a shallow copy. A deep copy is where any referenced objects
are also copied - and perhaps (if implemented that way) references to their
ref... etc.
In Character constants, you might like to demonstrate embedded double
quotes.
Gossip = "So she said ""with a kipper"". No joking - that''s what she
said..."

There''s also:
Dim InitialLetter As Char = "F"c
If InitialLetter >= "A"c And InitialLetter <= "Z"c Then ...
You''ve got
age = Convert.ToInt32(Console.ReadLine())
There''s still
age = Val (Console.ReadLine())
which is much more forgiving, especially for unchecked console input.
Phew. I bit off more than I thought there!!

I''d suggest posting this to these groups as well. They must get lots of
conversion queries too.
languages.csharp
dotnet.general
dotnet.academic

Regards,
Fergus


感谢Tom和Fergus的出色评论。 />
我会很快做出一些修正。


Frank
Thank you Tom and Fergus for your excellent comments.
I''ll make some corrections very soon.

Frank


这篇关于VB.NET和C#比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆