Converting to string returns null
while I am trying to convert the return value of the add method to a string, it doesnt return any value in console.while I am removing the tostring method that returns a value. If I write any character inside the double quote displayed in the console, what happens when I call the tostring method? if i didn't put any double quote as parameter it will show compile time error like (specify the culture of the string) what is the purpose of specifying the culture when converting int to string? I think I can convert an integer value to a string by calling the tostring method, why can't I do the 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();
}
thank.
All about implementation;
If the format is null or an empty string (") , the return value of this instance is formatted with the common number format specifier (" G ").
That's why it's .ToString("")
equal .ToString()
, because
The ToString () method formats the default Int32 value ("G" or generic) using the current culture's NumberFormatInfo object.
I tried all cultures in .ToString("")
and out of culture , returns null
or empty string.
foreach (var c in CultureInfo.GetCultures(CultureTypes.AllCultures))
{
if((77).ToString("G", c) != "77")
Console.WriteLine (c.Name);
}
The blue line is probably there is a plugin (possibly ReSharper) that warns you to use other overloads that take CultureInfo
as a parameter, eg.
source to share
just specify your string culture as string.empty
Console.WriteLine(Add(23,54).ToString(string.Empty));
Console.ReadKey();
Culture name: "" (empty string)
Culture Id: 0x007F
Language-Country / Region: invariant culture
http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo%28v=vs.71%29.aspx
string.Empty is a read-only field, whereas "" is a compile-time constant. Some places behave differently.
source to share