What is the use of the := syntax?

大憨熊 提交于 2019-12-17 20:47:19

问题


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?


回答1:


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

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?

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 to ref and out in C#, i.e. it allows the method to manipulate the parameter itself.




回答3:


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

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.



来源:https://stackoverflow.com/questions/902367/what-is-the-use-of-the-syntax

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!