:= 语法有什么用?
我是一名从事 VB.NET 项目的 C# 开发人员,当我调用带有 ByRef 参数的函数时,VS 一直试图让我使用 := thingie,如下所示:
While reader.Read()
HydrateBookFromReader(reader:=???)
HydrateBookFromReader 函数具有以下签名:
Public Function HydrateBookFromReader(ByRef reader As SqlDataReader) As Book
Why does intellisense一直坚持我使用 := 结构,它的用途是什么?
I'm a C# developer working on a VB.NET project, and VS keeps trying to get me to use the := thingie when I call a function with a ByRef parameter like so:
While reader.Read()
HydrateBookFromReader(reader:=???)
the HydrateBookFromReader function has the following signature:
Public Function HydrateBookFromReader(ByRef reader As SqlDataReader) As Book
Why does intellisense keep insisting that I use that := construction, and what is it for?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 VB 中,:= 用于指定命名参数。
这对于指定可选参数特别有用。
In VB, the := is used in specifying named parameters.
This is especially useful for specifying optional parameters.
值得注意的是,IntelliSense 并不是坚持,而是建议。 在您的情况下使用它是没有意义的……此功能主要用于带有许多可选参数的很长的参数列表,您只想传递其中的最后一个参数。 使用 Microsoft Office Interop 时它非常有用。
另外(因为您在标签中提到了它):这与
ByRef
无关。ByRef
相当于C# 中的ref
和out
,即它允许方法操作参数本身。It's important to note that IntelliSense doesn't insist, it proposes. Using it in your case wouldn't make sense … this feature is primarily used for very long parameter lists with many optional parameters, of which you only want to pass, say, the last one. It's useful when working with Microsoft Office Interop.
Also (since you mention it in your tags): this has got nothing to do with
ByRef
.ByRef
is equivalent toref
andout
in C#, i.e. it allows the method to manipulate the parameter itself.智能感知可能会建议使用 := 语法,但我怀疑没有它它也能编译。
在允许使用可选参数的 C# 未来版本中,命名参数将允许您指定某些参数但不能指定其他参数,并且可以按照与声明的顺序不同的顺序指定参数。 命名参数还允许您有选择地阐明传入参数的用途,使代码在某些情况下更具可读性。
命名参数在 c# 4.0 中对于 COM Interop 尤为重要,其中许多多余的参数可以被消除。
Anders Hejlsberg 在第 9 频道上对 C# 的未来进行了精彩的讨论,网址为 http://channel9.msdn。 com/pdc2008/TL16/。 他对命名参数的讨论是在演讲进行到 40 分 45 秒时进行的。
Intellisense may be suggesting the := syntax, but I suspect that it will compile without it.
In future versions of C# where optional parameters are allowed, named parameters will allow you to specify some parameters but not others, and to specify parameters in a different order than they were declared. Named parameters will also allow you to optionally clarify the purpose of the parameter being passed in, making the code more readable in some cases.
Named parameters will be especially important in c# 4.0 for COM Interop, where many superfluous parameters can be eliminated.
Anders Hejlsberg has an excellent discussion about the future of C# on Channel 9 at http://channel9.msdn.com/pdc2008/TL16/. His discussion about named parameters is at 40 minutes and 45 seconds into the talk.