dotnet / dotnet/vblang

Passing Nothing as the value of a ParamArray parameter.

Open
#348 4 comments 0 reactions 0 assignees View on GitHub
Dominant language
No language data
Stars
328
Forks
71
PR merge metrics
No merged PRs in 30d

Description

Follow up on @KathleenDollard's comment on [#8060](https://github.com/dotnet/docs/issues/8060).

> Have you seen this actually cause a bug in real code? I'm wondering if we need more here than the documentation fix - specifically do we need an analyzer to check for this? Feel free to follow up on this in vblang as it isn't a docs issue

I was looking at a function in my code that has a `ParamArray` parameter populated with data taken from a network request. Because the network is inherently "unsafe" and rogue clients can send malformed data, I found myself wondering **what happens if I get the value Nothing for this `ParamArray` parameter?** That's when I went to the [docs ](https://docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/procedures/parameter-arrays#calling-a-paramarray)and found the statement:

> Nothing — that is, you can omit the ParamArray argument. In this case, an empty array is passed to the procedure. **You can also pass the Nothing keyword, with the same effect.**

...which is of course incorrect as mentioned in [#8060](https://github.com/dotnet/docs/issues/8060).

In practice it seems that most people just omit values when they have nothing to pass to a method with a `ParamArray` parameter, instead of explicitly passing `Nothing` which would cause a null reference exception if the called code doesn't check for this (as was shown in the [Rosyln example](https://github.com/dotnet/roslyn/blob/fab7134296816fc80019c60b0f5bef7400cf23ea/src/VisualStudio/Core/Test/ObjectBrowser/Helpers.vb#L82)).

The most famous function with a `ParamArray` parameter is probably `String.Format()` and looking at the code, [it does check for null](https://referencesource.microsoft.com/#mscorlib/system/string.cs,3004) but **I suspect there is a great deal of code out there which doesn't, so having a compiler warning (or at least a built-in analyzer/refactoring fix) would be useful (especially for public methods).**

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.