为什么Main方法有“Overloads”这个词?在VB中?

发布于 2024-11-29 07:45:47 字数 387 浏览 0 评论 0原文

来自 C#...

我正在查看这个网站

http://www.harding.edu/fmccown /vbnet_csharp_comparison.html

并注意到它说

public static void Main(string[] args) {

相当于

Overloads Shared Sub Main(ByVal args() As String) 

那么...“重载”是关于什么的?

Coming from C#...

I was looking at this website

http://www.harding.edu/fmccown/vbnet_csharp_comparison.html

And noticed that it said

public static void Main(string[] args) {

is equivalent to

Overloads Shared Sub Main(ByVal args() As String) 

So... what is that "Overloads" all about?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

顾忌 2024-12-06 07:45:47

这是一个错误,没有任何东西超载。任何地方都没有预制的 Sub Main(),尤其是在名为 HelloWorld 的类中。它恰好可以工作,因为 vb.net 编译器对此不是很挑剔。将此代码粘贴到一个类中,您可以亲自查看:

Class Test
    Overloads Sub IdontOverloadAnything()

    End Sub
End Class

VB.NET 编译器往往会创造奇迹。这不是其中一种情况,Main() 的魔力在 CLR 中。大多数 C# 程序员都会认为这是该语言中的一个错误。我不能不同意。

It is a mistake, nothing is getting overloaded. There is no pre-baked Sub Main() anywhere, especially not in a class named HelloWorld. It happens to work because the vb.net compiler isn't very picky about it. Paste this code in a class to see for yourself:

Class Test
    Overloads Sub IdontOverloadAnything()

    End Sub
End Class

The VB.NET compiler tends to make magic happen. This is not one of those cases, the Main() magic is in the CLR. Most any C# programmer would consider this a bug in the language. I can't disagree.

漫雪独思 2024-12-06 07:45:47

因为标准 Sub Main 没有参数,并且您使用以下命令重载它带参数的新主程序。

Because the standard Sub Main has no parameters and you are overloading it with the new Main procedure with parameters.

坏尐絯℡ 2024-12-06 07:45:47

在 VB6 中,与大多数“古老”编程语言一样,有一句格言“只能有一个!” (参见电影《高地人》)。模块或类中只能有一个同名的 Function 或 Sub。在 VB.NET 中,就像在 C# 中一样,您可以拥有多个具有相同名称的方法,只要它们具有不同的签名即可。这意味着它们需要具有不同数量的参数或不同类型的参数。这些函数被称为重载。在 VB 中,您可以向此类函数或子函数添加可选关键字Overloads

Public Sub Test(s As String)
Public Sub Test(i As Integer)
Public Sub Test(s As String, i As Integer)

这样就可以了。然而

Public Sub Test(t As String)

不会,因为已经存在一个带有一个字符串参数的重载方法。不同的参数名称是不够的。

In VB6, as in most "ancient" programming languages, there was the maxim "There can be only one!" (see the movie "Highlander"). You could have only one Function or Sub with the same name in a module or in a class. In VB.NET as in C# you can have several methods having the same name, as long as they have different signatures. This means that they need to have a different number of parameters or differnt types of parameters. These function are then said to be overloaded. In VB you can add the optional keyword Overloads to such Functions or Subs.

Public Sub Test(s As String)
Public Sub Test(i As Integer)
Public Sub Test(s As String, i As Integer)

This would be OK. However

Public Sub Test(t As String)

would not, since there exists already a overloaded method with one string parameter. Different parameter names are not sufficient.

南巷近海 2024-12-06 07:45:47

因为有一个标准的 Shared Sub Main 并且您正在超载它。您可以忽略重载。

Because there is a standard Shared Sub Main and you are overloading it. You can ommit the overloads.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文