Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need for String.Format in C# to throw compile time error for mismatch in arguments

Is there any way to to add custom compile time errors when there is a mismatch in number of arguments passed to String.Format in C#

Eg:

String.Format("{0} and {1} are my pets", animal);

the above line should throw an error since we have passed only one argument "animal" , while it is expecting two {0},{1}.. but in reality it throws just run time errors.

like image 638
Vivin joy Avatar asked Jan 17 '23 20:01

Vivin joy


2 Answers

FxCop / Code Analysis will pick this up. And yes, you can do that at compile time in VS Premium / Ultimate.

enter image description here

like image 132
Craig Stuntz Avatar answered Jan 23 '23 03:01

Craig Stuntz


Resharper will add an IDE warning for that. However, that isn't a compiler error; what you have written is perfectly fine as far as the compiler is concerned.

enter image description here

like image 24
Marc Gravell Avatar answered Jan 23 '23 04:01

Marc Gravell