可选参数 - 真正发生了什么? [英] Optional Paramters- What really occurs?

查看:52
本文介绍了可选参数 - 真正发生了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚拿起John Robbins的调试书副本,开始在反汇编代码中查看

。无论如何,我讨厌VB中的可选参数,但是我检查它们是为了看看IL是什么创建的。我之前用

模块完成了这项工作,并看到< gasp>它们的行为就像只有

静态成员的密封类。


无论如何,看起来可选参数只不过是告诉
的一种方式
编译器为你写一些重载。所以,在子程序的情况下,我看到b $ b看到有15个可选参数(不,我没写过)

它看起来像IL显示了15种重载方法。我绝不是一个

专家,但如果是这样的话......我是否正确,这有点伤痕?

我提到过对于那个写它的人来说,他说我离开了我的摇杆。

据他说,这是VB.NET优于C#的好处之一,因为你

不必使用广泛的重载,而且效率更高。从

IL来看,这看起来恰恰相反。


因此,如果没有激发VB.NET与C#的争论,我是否会写这个或者是我好吗?b $ b读错了吗?


谢谢,


比尔

I just picked up a copy of John Robbins'' debugging book and started to look
at disassembled code. Anyway, I hate optional Parameters in VB, but I was
checking them out to see what IL is created. I''ve done this before with
Modules, and saw that <gasp> they behave just like sealed classes with only
static members.

Anyway, it looks like Optional Parameters are nothing but a way to tell the
compiler to write some overloads for you. So, in the case of a Subroutine I
was looking at that had 15 optional parameters (and no, I didn''t write it)
it looked like the IL was showing 15 overloaded methods. I''m by no means an
Expert, but if this is the case...am I right in that it''s kind of scarry?
I mentioned this to the guy who wrote it and he said I''m off of my rocker.
According to him, this is one of the benefits of VB.NET over C# in that you
don''t have to use extensive overloading and it''s more efficient. From the
IL though, this looks like the opposite.

So without stirring a VB.NET vs. C# debate, am I write about this or am I
reading it wrong?

Thanks,

Bill

推荐答案



" William Ryan" <做******** @ nospam.comcast.net>在消息中写道

news:ud **************** @ TK2MSFTNGP12.phx.gbl ...

"William Ryan" <do********@nospam.comcast.net> wrote in message
news:ud****************@TK2MSFTNGP12.phx.gbl...
我刚刚选中一份John Robbins的调试书,并开始以
查看反汇编代码。无论如何,我讨厌VB中的可选参数,但我正在检查它们以查看IL的创建。我之前用
模块完成了这项工作,并且看到了< gasp>它们的行为就像密封类一样只有
的静态成员。

无论如何,看起来可选参数只不过是告诉
编译器为你编写一些重载的方法。因此,在Subroutine
的情况下,我看到它有15个可选参数(不,我没有写它)
看起来IL显示了15个重载方法。我绝不是
专家,但如果是这样的话......我是否正确,这有点伤痕?
我向写这篇文章的人提到了这一点他说我离开摇杆了。
根据他的说法,这是VB.NET优于C#的好处之一,因为
你不必使用广泛的重载而且它'效率更高。尽管如此,这看起来恰恰相反。
从我从ildasm看到的,可选关键字没有提供

重载(至少不在此编译器中),而是插入可选值

值直接代码。

我写了这个小程序来玩选项:

模块模块1

Sub Main()

Dim X As Class1 = New Class1

X.Cat(,meow)

X.Dog()

End Sub

结束模块

Class Class1

公共函数Cat(可选ByVal S As String =" neko",可选ByVal

sound As String =" Nyao")As Boolean

Console.WriteLine(S + sound)

结束函数

Public Function Dog(可选ByVal S As String =" inu")As Boolean

Console.WriteLine(S)

结束功能

结束课


然后我在ildasm打开它,它显示了Dog和Cat的每个实例,并且

反汇编的电话l对于X.Cat(,Meow)的ine是:

IL_0008:ldstr" neko"

IL_000d:ldstrmeow"

IL_0012:callvirt实例bool VBOptionalTest.Class1 :: Cat(字符串,

字符串)


for X.Dog()它是:

IL_0019:ldstr" inu"

IL_001e:callvirt实例bool VBOptionalTest.Class1 :: Dog(字符串)


可选关键字出现在直接将值嵌入到调用

代码中,使可选参数值更改二进制分解更改。


之前我听过这个,但这是第一个时间我实际上已经验证了

它。
因此,在没有激发VB.NET与C#辩论的情况下,我是在写这个还是我读错了?

谢谢,

比尔
I just picked up a copy of John Robbins'' debugging book and started to look at disassembled code. Anyway, I hate optional Parameters in VB, but I was
checking them out to see what IL is created. I''ve done this before with
Modules, and saw that <gasp> they behave just like sealed classes with only static members.

Anyway, it looks like Optional Parameters are nothing but a way to tell the compiler to write some overloads for you. So, in the case of a Subroutine I was looking at that had 15 optional parameters (and no, I didn''t write it)
it looked like the IL was showing 15 overloaded methods. I''m by no means an Expert, but if this is the case...am I right in that it''s kind of scarry?
I mentioned this to the guy who wrote it and he said I''m off of my rocker.
According to him, this is one of the benefits of VB.NET over C# in that you don''t have to use extensive overloading and it''s more efficient. From the
IL though, this looks like the opposite. From what I''m seeing from ildasm, the optional keyword doesn''t provide
overloads (atleast not in this compiler), but instead inserts optional value
values directly into code.
I wrote this little program to play with optionals:
Module Module1
Sub Main()
Dim X As Class1 = New Class1
X.Cat(, "meow")
X.Dog()
End Sub
End Module
Class Class1
Public Function Cat(Optional ByVal S As String = "neko", Optional ByVal
sound As String = "Nyao") As Boolean
Console.WriteLine(S + sound)
End Function
Public Function Dog(Optional ByVal S As String = "inu") As Boolean
Console.WriteLine(S)
End Function
End Class

I then opened it in ildasm, it showed one instance each of Dog and Cat, and
the disassembled call line for X.Cat(,"Meow) was:
IL_0008: ldstr "neko"
IL_000d: ldstr "meow"
IL_0012: callvirt instance bool VBOptionalTest.Class1::Cat(string,
string)

for X.Dog() it was:
IL_0019: ldstr "inu"
IL_001e: callvirt instance bool VBOptionalTest.Class1::Dog(string)

the optional keyword appears to directly embed the values into the calling
code, making optional arguments value changes a binary breaking change.

I had heard this before, but this is the first time i''ve actually verified
it.
So without stirring a VB.NET vs. C# debate, am I write about this or am I
reading it wrong?

Thanks,

Bill





" ;威廉瑞恩 <做******** @ nospam.comcast.net>在消息中写道

news:ud **************** @ TK2MSFTNGP12.phx.gbl ...

"William Ryan" <do********@nospam.comcast.net> wrote in message
news:ud****************@TK2MSFTNGP12.phx.gbl...
我刚刚选中一份John Robbins的调试书,并开始以
查看反汇编代码。无论如何,我讨厌VB中的可选参数,但我正在检查它们以查看IL的创建。我之前用
模块完成了这项工作,并且看到了< gasp>它们的行为就像密封类一样只有
的静态成员。

无论如何,看起来可选参数只不过是告诉
编译器为你编写一些重载的方法。那么,在子程序的情况下



忘记提及,我认为如果编译器确实会更好

生成重载自动而不是隐式强制调用

代码嵌入值,但我没有设计语言。

不能说我会反对到一个C#可选参数系统,它只是一个手动写大量重载的快捷方式,假设它是在边界内编写的,但是我不会特别兴奋使用VB方法。

不要用一种语言来颂扬另一种语言,我只是不喜欢这种特殊的

设计用任何语言,从来没有。

看着有15个可选参数(不,我没写过)
看起来IL显示了15个重载方法。我绝不是
专家,但如果是这样的话......我是否正确,这有点伤痕?
我向写这篇文章的人提到了这一点他说我离开摇杆了。
根据他的说法,这是VB.NET优于C#的好处之一,因为
你不必使用广泛的重载而且它'效率更高。虽然从IL来看,这看起来恰恰相反。

因此,如果没有激发VB.NET与C#的争论,我是否会写这个或者我是错误地读错了?

谢谢,

比尔
I just picked up a copy of John Robbins'' debugging book and started to look at disassembled code. Anyway, I hate optional Parameters in VB, but I was
checking them out to see what IL is created. I''ve done this before with
Modules, and saw that <gasp> they behave just like sealed classes with only static members.

Anyway, it looks like Optional Parameters are nothing but a way to tell the compiler to write some overloads for you. So, in the case of a Subroutine I

Forgot to mention, I think it would have been better if the compiler did
generate overloads automatically instead of implicitly forcing the calling
code to embed the values, but I didn''t design the langauge.
Can''t say I''d be opposed to a C# optional parameter system that is simply a
shortcut to manually writing lots of overloads, assuming it was written
within bounds, but I wouldn''t be particulary thrilled with the VB method.
Not to extol one language over the other, I just don''t like this particular
design in any language, never have.
was looking at that had 15 optional parameters (and no, I didn''t write it)
it looked like the IL was showing 15 overloaded methods. I''m by no means an Expert, but if this is the case...am I right in that it''s kind of scarry?
I mentioned this to the guy who wrote it and he said I''m off of my rocker.
According to him, this is one of the benefits of VB.NET over C# in that you don''t have to use extensive overloading and it''s more efficient. From the
IL though, this looks like the opposite.

So without stirring a VB.NET vs. C# debate, am I write about this or am I
reading it wrong?

Thanks,

Bill



丹尼尔:


我非常感谢这篇文章。我没有把IDE放在我面前,但是这里是我记得的最好的东西。在您的cat示例中,如果您传入Neko,则需要
。对于S而言,没有声音,s:=" Neko"或传入

声音但不是S,更不用说三个电话,一个有两个参数,一个用

第一个参数而不是第二个,一个用第二个param但不是

第一个,看起来有三个不同的电话。我希望我能在我面前拆卸

(可能应该放弃发布直到我这样做)

但它看起来真的很奇怪b / c它看起来像每个案件是

写的不同。让我回头看看。


再次感谢,


比尔


Daniel O''Connell <有关****** @ comcast.net>在留言中写道

news:dHK8b.333117
Daniel:

I totally appreciate the post. I don''t have the IDE in front of me but
here''s what I remember off of the top of my head. In your example for cat,
if you pass in "Neko" for S and nothing for sound, s:="Neko" or pass in
sound but not S, let alone make three calls, one with both params, one with
the first param but not the second, and one with the second param but not
the first, It looked like there were three different calls. I wish I had
the disassembly in front of me (and probably should quit posting until I do)
but it just seemed really weird b/c it looked like each of the cases was
being written differently. Let me check back when I get to wrok.

Thanks Again,

Bill

"Daniel O''Connell" <on******@comcast.net> wrote in message
news:dHK8b.333117


这篇关于可选参数 - 真正发生了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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