Should missing format string parameters be a compile-time error?

In the case where the full text of the format string is static and known at compile time, should the parameter of the format string be absent to be a compile-time error, or at least a warning?

ReSharper catches this, but it is just an underlined curve. I got the impression that this would cause a general compile-time error:

string x = string.Format("soeuotnh {0}");

Is there a way to raise an error warning of this type without having to run my code through FxCop or something like that? Even C / C ++ compilers raise a warning / error for such an obvious error (although they usually do not check type safety).

+3
source share
7 answers

Microsoft Connect, ,

  • , : " , , ", , , , , , #.
  • , , (-) , . , , ( ) , , .
  • , , , , , - , , ( ), , .
  • , C , - .
  • , , , , ( ).

", , , , , " . , ( ) -, , , ( , const), (), . Visual Studio , MS Connect, : , ... VS, , - .

-2

, ? , , , String.Format #.

C; undefined - , , ( ...), , . "", .

+2

, - , , . , Format (String, params Object[]), :

public static string Format(
    string format,
    params Object[] args
)

: System.String
(. ).

: System.Object [] , .

params ( ), .

Edit:

, . , , Resharper, , .

+2

C , , , , , . #, , , .

, string.Format , ( ) - #. , , .

, , , . , , , . .

0

, , .

, - :

string format = "{0}";
List<string> parms = new List<string> { "Hello" };

if (DateTime.Now.Second % 2 == 0)
{
    format += " {1}";
    parms.Add("World");
}

Console.WriteLine(format, parms.ToArray());
0

Resharper : " ".

0

"format" . , , .

, ... (!)

.

: . , , . , : .

, String.Format(, , ), ... .

-1

All Articles