Reputation: 41
Hey guys I'm sure if this had been asked already, but I could not find it. If it's redundant I'm sorry. Please link me to it. My question is:
What does error "Argument is not used in format string" mean? I am using C#
using System;
class PrintNum
{
static void Main()
{
short s = 10;
int i = 1000;
long l = 1000000;
float f = 230.47f;
double d = 30949.374;
//This is where I'm getting the error.
Console.Write("s: %d\n", s); //<<< if you hover over the variable s on the outside.
Console.Write("i: %d\n", i);
Console.Write("l: %ld\n", l);
Console.Write("f: %.3f\n", f);
Console.Write("d: %.3f\n", d);
}
}
Upvotes: 1
Views: 3669
Reputation: 3575
Like the other answers says, your Console.Write needs to include "{0}" as that is the placeholder for the variable your are writing. It's important to note that {0}, {1}, etc, are positional parameters and that order must be followed inside your string. It's also important to know that Console.Write uses String.Format underneath so you can use use all the format strings described at Standard Numeric Format Strings (https://msdn.microsoft.com/en-us/library/dwhawy9k(v=vs.110).aspx.) That being said, I think this is what you are looking for:
sing System;
public class Test
{
public static void Main()
{
short s = 10;
int i = 1000;
long l = 1000000;
float f = 230.47f;
double d = 30949.374;
Console.WriteLine("s: {0:D}", s);
Console.WriteLine("i: {0:D}", i);
Console.WriteLine("l: {0:D}", l);
Console.WriteLine("f: {0:F}", f);
Console.WriteLine("d: {0:F}", d);
}
}
Output:
s: 10
i: 1000
l: 1000000
f: 230.47
d: 30949.37
Upvotes: 0
Reputation: 25370
use {0}
to plug your argument into the string, 0
being the argument number:
Console.Write("s: {0}\n", s);
Check the documentation if you want to further format your number.
Also, there is a Console.WriteLine
method that will save you from adding line breaks at the end of your string.
Upvotes: 0
Reputation: 26846
Your format string is incorrect. It should be something like
Console.Write("s: {0}\n", s);
where {0}
in format string stands for "first parameter passed after format string", {1}
is for second parameter (if any) and so on.
Unlike in C, %d
and similar formatting parameters are not used in C# format strings and this formatting are handled with ToString
method override of types you're using.
Upvotes: 3