Search code examples
c#arraysstringvarimplicit

Printing Implicit Arrays


I have searched for this but I haven't got my answer .So I am here. okay , so here is the code

var contacts = new[] 
{
    new
    {
        Name = " Eugene Zabokritski",
        PhoneNumbers = new[] { "206-555-0108", "425-555-0001" }
    },
    new
    {
        Name = " Hanying Feng",
        PhoneNumbers = new[] { "650-555-0199" }
    }
};

I want to print out the Phonenumbers but when I try I get System.String[] instead. I tried foreach loop as well . So what's the way to print out the Name as well as Phone Number?


Solution

  • Make use of string.Join here:

    foreach(var contact in contacts)
    {
       Console.WriteLine("Name: {0}", contact.Name);
       Console.WriteLine("PhoneNumbers: {0}", string.Join(",",contact.PhoneNumbers));
    }