3

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.

3 Answers3

3

Use ToString with no parameters

Add(23,54).ToString()

Using the parameter you specified you set a culture for the string conversion. More here.

Moti Azu
  • 5,392
  • 1
  • 23
  • 32
  • if i didn't put any double quote it is showing compile time error like (specify culture of string) what is the purpose of specifying culture while converting int to string? Read the question. – mybirthname Oct 26 '14 at 17:06
  • My guess is the guy didn't want anything to do with culuture, but since he used it I thought an explanation would be nice. This code compiles without the double quote. – Moti Azu Oct 26 '14 at 17:08
  • @mot yes it is working but there occuring a blue line under tostring that line is because,i didn't specified the culture of string.if i put a ("") it's not showing error but nothing i can see in the console/ –  Oct 26 '14 at 17:16
  • 3
    That blue line probably comes as a warning from resharper. You can read about it here http://stackoverflow.com/questions/22887804/why-specify-culture-in-string-conversion – Moti Azu Oct 26 '14 at 17:17
3

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.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
0

simply specify your culture of string as string.empty

            Console.WriteLine(Add(23,54).ToString(string.Empty));
            Console.ReadKey();

Culture Name:"" (empty string)

Culture Identifier: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 they behave differently.

MMM
  • 3,132
  • 3
  • 20
  • 32
  • What? o.O There is no difference to use `""` and `string.Empty` here. _Even_ if could, `string.Empty` wasn't specify any culture to using only itself. – Soner Gönül Oct 26 '14 at 19:02
  • @SonerGönül i saw this article from msdn.string.Empty is a read-only field whereas "" is a compile time constant – MMM Oct 26 '14 at 19:30