0

C# Beginner Question

I’m trying to create my first array of names and although I’m not getting a compiling error, the names won’t show when I run the console. Any suggestions are really appreciated. namespace ConsoleApp12 { class Program { static void Main(string)[] args) { String [] names; names = new String[2]; names [0] = “John”; names [1] = “Bob”; Console.WriteLine(names); } } }

13th Aug 2018, 8:57 PM
Alex
Alex - avatar
7 odpowiedzi
0
try ``` Console.WriteLine(names[0]); ``` to output all names inside the array
13th Aug 2018, 9:00 PM
Emmanuel Lanuzo
Emmanuel Lanuzo - avatar
0
If you write onli names to Console.WriteLine(); than the output is an error because you want to write out a string[] like string To fix this problem I advise this: Console.WriteLine(names[0]); Console.WriteLine(names[1]); Or if you want to write out the full array than: int i = 0; while (i < names.length) { Console.Write(names[i]); i++; } I hope I can help
13th Aug 2018, 9:19 PM
Ádám Vilányi
Ádám Vilányi - avatar
0
Hi Adam, thank you. I followed your suggestion and I get a repeat of the name John. I’m trying to get it to do this: John Bob To print the names in that order.
13th Aug 2018, 9:45 PM
Alex
Alex - avatar
0
foreach(string s in names) Console.WriteLine(s); this should work fine
13th Aug 2018, 10:30 PM
hinanawi
hinanawi - avatar
0
Hinawani, thank you for your help. When I run console after your suggestion it just runs and doesnt print the names. I’m not getting any errors it just runs console.
13th Aug 2018, 11:17 PM
Alex
Alex - avatar
0
https://code.sololearn.com/cl3oe32E92z9/?ref=app replace your entire code with this, and see if it works Alex
14th Aug 2018, 7:09 AM
hinanawi
hinanawi - avatar
0
Thank you.
14th Aug 2018, 11:49 AM
Alex
Alex - avatar