有什么用:=语法?

时间:2022-03-13 22:35:11

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:

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

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

the HydrateBookFromReader function has the following signature:

HydrateBookFromReader函数具有以下签名:

Public Function HydrateBookFromReader(ByRef reader As SqlDataReader) As Book

Why does intellisense keep insisting that I use that := construction, and what is it for?

为什么intellisense一直坚持我使用它:=构造,它的用途是什么?

3 个解决方案

#1


In VB, the := is used in specifying named parameters.

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

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

This is especially useful for specifying optional parameters.

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

#2


Why does intellisense keep insisting that I use that := construction, and what is it for?

为什么intellisense一直坚持我使用它:=构造,它的用途是什么?

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.

重要的是要注意IntelliSense并不坚持,它提出。在你的情况下使用它是没有意义的...这个功能主要用于很长的参数列表,有许多可选参数,你只想通过,比如最后一个。在使用Microsoft Office Interop时很有用。

Also (since you mention it in your tags): this has got nothing to do with ByRef. ByRef is equivalent to ref and out in C#, i.e. it allows the method to manipulate the parameter itself.

另外(因为你在标签中提到它):这与ByRef无关。 ByRef等同于C#中的ref和out,即它允许该方法操纵参数本身。

#3


Intellisense may be suggesting the := syntax, but I suspect that it will compile without it.

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.

在允许使用可选参数的C#的未来版本中,命名参数将允许您指定一些参数而不指定其他参数,并以与声明的顺序不同的顺序指定参数。命名参数还允许您有选择地阐明传入参数的目的,使代码在某些情况下更具可读性。

Named parameters will be especially important in c# 4.0 for COM Interop, where many superfluous parameters can be eliminated.

对于COM Interop,命名参数在c#4.0中尤其重要,因为可以消除许多多余的参数。

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.

Anders Hejlsberg在第9频道的http://channel9.msdn.com/pdc2008/TL16/上对C#的未来进行了精彩的讨论。他关于命名参数的讨论是在谈话的40分45秒。

#1


In VB, the := is used in specifying named parameters.

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

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

This is especially useful for specifying optional parameters.

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

#2


Why does intellisense keep insisting that I use that := construction, and what is it for?

为什么intellisense一直坚持我使用它:=构造,它的用途是什么?

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.

重要的是要注意IntelliSense并不坚持,它提出。在你的情况下使用它是没有意义的...这个功能主要用于很长的参数列表,有许多可选参数,你只想通过,比如最后一个。在使用Microsoft Office Interop时很有用。

Also (since you mention it in your tags): this has got nothing to do with ByRef. ByRef is equivalent to ref and out in C#, i.e. it allows the method to manipulate the parameter itself.

另外(因为你在标签中提到它):这与ByRef无关。 ByRef等同于C#中的ref和out,即它允许该方法操纵参数本身。

#3


Intellisense may be suggesting the := syntax, but I suspect that it will compile without it.

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.

在允许使用可选参数的C#的未来版本中,命名参数将允许您指定一些参数而不指定其他参数,并以与声明的顺序不同的顺序指定参数。命名参数还允许您有选择地阐明传入参数的目的,使代码在某些情况下更具可读性。

Named parameters will be especially important in c# 4.0 for COM Interop, where many superfluous parameters can be eliminated.

对于COM Interop,命名参数在c#4.0中尤其重要,因为可以消除许多多余的参数。

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.

Anders Hejlsberg在第9频道的http://channel9.msdn.com/pdc2008/TL16/上对C#的未来进行了精彩的讨论。他关于命名参数的讨论是在谈话的40分45秒。