while am trying to convert the return value of add method to string it is not returning any value in console.while i remove the tostring method it is returning value.if i write any character inside the double quote it is showing in console. what is happening while am calling tostring method? if i didn't put any double quote as parameter it is showing compile time error like (specify culture of string) what is the purpose of specifying culture while converting int to string? i think i can convert integer value to string by calling tostring method,why can't i do conversion in this scenario?
private static int Add(int x,int y)
{
return x+y;
}
static void Main(string[] args)
{
Console.WriteLine(Add(23,54).ToString(""));
Console.ReadKey();
}
thanks.
It's all about implementation;
From Int32.ToString(string)
If format is null or an empty string (""), the return value of this instance is formatted with the general numeric format specifier ("G").
That's why .ToString("") is equal to .ToString() because
From Int32.ToString()
The ToString() method formats an Int32 value in the default ("G", or general) format by using the NumberFormatInfo object of the current culture.
I tried all cultures to format with .ToString("") and no culture returns null or empty string.
foreach (var c in CultureInfo.GetCultures(CultureTypes.AllCultures))
{
if((77).ToString("G", c) != "77")
Console.WriteLine (c.Name);
}
Blue line probably there is a plugin (maybe ReSharper) that warn you to use another overloads that takes CultureInfo as a parameter for example.
Use ToString with no parameters
Add(23,54).ToString()
Using the parameter you specified you set a culture for the string conversion. More here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With