为什么Main方法有“Overloads”这个词?在VB中?
来自 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是一个错误,没有任何东西超载。任何地方都没有预制的 Sub Main(),尤其是在名为 HelloWorld 的类中。它恰好可以工作,因为 vb.net 编译器对此不是很挑剔。将此代码粘贴到一个类中,您可以亲自查看:
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:
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.
因为标准 Sub Main 没有参数,并且您使用以下命令重载它带参数的新主程序。
Because the standard Sub Main has no parameters and you are overloading it with the new Main procedure with parameters.
在 VB6 中,与大多数“古老”编程语言一样,有一句格言“只能有一个!” (参见电影《高地人》)。模块或类中只能有一个同名的 Function 或 Sub。在 VB.NET 中,就像在 C# 中一样,您可以拥有多个具有相同名称的方法,只要它们具有不同的签名即可。这意味着它们需要具有不同数量的参数或不同类型的参数。这些函数被称为重载。在 VB 中,您可以向此类函数或子函数添加可选关键字
Overloads
。这样就可以了。然而
不会,因为已经存在一个带有一个字符串参数的重载方法。不同的参数名称是不够的。
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.This would be OK. However
would not, since there exists already a overloaded method with one string parameter. Different parameter names are not sufficient.
因为有一个标准的 Shared Sub Main 并且您正在超载它。您可以忽略重载。
Because there is a standard Shared Sub Main and you are overloading it. You can ommit the overloads.