1. Home
  2. Computing & Technology
  3. C / C++ / C#
photo of David Bolton

David's C / C++ / C# Blog

By David Bolton, About.com Guide to C / C++ / C#

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() ;
        }
    }
}

Comments

May 7, 2008 at 7:30 am
(1) renato lacerda says:

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

May 7, 2008 at 10:25 am
(2) Peter Jansson says:

In my world it should not compile, but then I am not a C# expert… :)

May 7, 2008 at 11:18 am
(3) Robert C. Cartaino says:

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

May 7, 2008 at 4:26 pm
(4) Lennie says:

David 45.60

May 7, 2008 at 4:54 pm
(5) Morgan says:

David 45.60

Or at least it should :)

Leave a Comment

Line and paragraph breaks are automatic. Some HTML allowed: <a href="" title="">, <b>, <i>, <strike>

Explore C / C++ / C#

More from About.com

  1. Home
  2. Computing & Technology
  3. C / C++ / C#

©2008 About.com, a part of The New York Times Company.

All rights reserved.