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); } } }
7 odpowiedzi
0
try
```
Console.WriteLine(names[0]);
```
to output all names inside the array
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
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.
0
foreach(string s in names)
Console.WriteLine(s);
this should work fine
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.
0
https://code.sololearn.com/cl3oe32E92z9/?ref=app
replace your entire code with this, and see if it works Alex
0
Thank you.