C# Programming Puzzle
Wednesday May 7, 2008
Answer tomorrow. Without trying it, will this compile and if so what does it output?
using System;
using System.Text;
namespace ex1
{
class Program
{
static void Main(string[] args)
{
string a="David ";
int b = 0;
a= string.Concat(a,4,5.6,b) ;
Console.WriteLine("a= {0}",a) ;
Console.ReadKey() ;
}
}
}
- Link to C# Tutorials


Comments
Let me try…
a=”David 45.6″
b is int thatīs why it doesnīt print.
Iīm I right??
Thankīs for the Puzzle David!!
Regards
Renato Lacerda
In my world it should not compile, but then I am not a C# expert…
I think it *should* output:
David 45.60
… because String.Concat() takes a list of strings *or* it can take a list of generic objects, regardless of their type. Integers are objects so Concat() would use the .ToString() method implemented for each object to extract the string representation.
So…
a.ToString() works the same way as
4.ToString() and
5.6.ToString() and
0.ToString().
Robert C. Cartaino
David 45.60
David 45.60
Or at least it should