Visual Studio 2019 IDE Warnings about String.Format Parameters

Viewed 377

Our team upgraded a large project developed using Visual Studio 2010 to Visual Studio 2019. One of the team members uses the String.Format(String, Object[]) and passes an array of objects to the method while the rest of the team uses the String.Format method by passing the objects as individual arguments.

This was just a matter of style preference when developing in Visual Studio 2010; but in Visual Studio 2019, any place that uses the String.Format(String, Object[]) that is passed an array is causing IDE warnings to appear.

Since the project is large, the number of these bogus warnings is numerous and going through the code to "fix" something that isn't broken is not preferable; however, these warnings do hide cases where the String format incorrectly matches the parameters (causing a runtime error).

Stranger yet, there are cases where these lines of code actually do throw String.Format exceptions if the machine running the application is not using .NET Framework 4.8. This doesn't make sense to me because this version of the String.Format method has been around since WELL before 4.8.

Here is an example of what I'm talking about.

Notice how there are 2 warnings on line 9 about the number of arguments passed to the String.Format method not matching the number expected in the format.

While on line 12 there are no warnings: because the String.Format method is being passed individual argument parameters instead of an array.

Does anyone have any insight into why this is causing problems using Visual Studio 2019 with .NET Version 4.8? Or have a suggestion on how to correct this problem (preferably one that doesn't require modifying every line of code the one team member used their style of calling the String.Format method).

enter image description here

Edit: Update, upon playing with this a bit more, I discovered that if the array is not declared inline (it is declared before hand), there are no errors. This worked when passing an array of strings and an array of objects.

This just makes the IDE warning even more bizarre....

enter image description here

0 Answers
Related