6

我是一名从事 VB.NET 项目的 C# 开发人员,当我调用带有 ByRef 参数的函数时,VS 一直试图让我使用 := thingie,如下所示:

While reader.Read()
HydrateBookFromReader(reader:=???)

HydrateBookFromReader 函数具有以下签名:

Public Function HydrateBookFromReader(ByRef reader As SqlDataReader) As Book

为什么智能感知一直坚持我使用 := 构造,它是做什么用的?

4

3 回答 3

10

在 VB 中,:= 用于指定命名参数。

Contact(Address:="2020 Palm Ave", Name:="Peter Evans")

这对于指定可选参数特别有用。

于 2009-05-23T20:11:29.400 回答
4

为什么智能感知一直坚持我使用 := 构造,它是做什么用的?

重要的是要注意 IntelliSense 不是坚持,而是建议。在您的情况下使用它没有意义……此功能主要用于带有许多可选参数的非常长的参数列表,您只想传递最后一个参数。它在使用 Microsoft Office Interop 时很有用。

另外(因为您在标签中提到它):这与ByRef. ByRef等效于 C# 中的refand out,即它允许方法操作参数本身。

于 2009-05-23T20:14:08.147 回答
1

Intellisense 可能建议使用 := 语法,但我怀疑没有它也可以编译。

HydrateBookFromReader(myReader);

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.

于 2009-05-23T20:21:45.617 回答