-2

What am I doing wrong? As a beginner I google this and everywhere the answer is the same. When I click on "fixes" inside the Microsoft Visual Studio they don't work at all.

My code below:

using System;

namespace Test.Net_Core
{
    class Program
    {
        static void Main(string[] args)
        {
            List<string> fruits = new List<string>();
            fruits.Add("apple");
            Console.WriteLine(fruits);
        }
    }
}
  • 1
    What is wrong? Were you expecting the each of the items to be printed? – Xiaoy312 Jan 28 '20 at 18:49
  • What exactly you expect from this code? And what you getting instead? (make sure to check https://stackoverflow.com/questions/42112051/c-sharp-print-list-of-string-array) – Alexei Levenkov Jan 28 '20 at 18:49
  • What is the actual issue you are facing? – Sathish Guru V Jan 28 '20 at 18:49
  • 1
    If you want to print `apple`, you should use `Console.WriteLine(fruits[0]);`, or use loop – Pavel Anikhouski Jan 28 '20 at 18:50
  • 1
    If you want to print all elements of the list with comma between then you can do it like thees `Console.WriteLine(string.Join(", ",fruits))` – s-s Jan 28 '20 at 19:22
  • You ask what you are doing *wrong*, but you have not said what the problem is. What's the problem? What question do you have that needs answering? – Eric Lippert Jan 28 '20 at 19:32
  • Sorry what is wrong is that I don't get anything printed out. Error codes as in CS0246 and CS0136 – ApexLegends Jan 29 '20 at 14:29
  • It would be helpful if you said what the text of those error messages was; we have not memorized the text of the error messages and it is tedious to look them up; help us help you! Your error is that you forgot `using System.Collections.Generic;` up top, which defines the `List` type. – Eric Lippert Jan 29 '20 at 21:43
  • I'm sorry Eric, I have found the solution by clicking a lot of buttons that showed solutions on the error. Unfortunately I can't recall what I did though. Thanks for all the help as a beginner in programming I'm trying to found out solutions myself to learn from it but it's confusing as hell haha. Now I'm trying to add an array to a list of int and I get this System.Collections.Generic.List`1[System.Int32]. So thats the next step to find out! – ApexLegends Jan 31 '20 at 15:45

1 Answers1

3

I assume what you mean is that when you do your WriteLine, you're getting something like

System.Collections.Generic.List`1[System.String]

when you really want the values of the list to be printed. First lets get into why what you're doing doesn't work. What should happen when you call ToString on a list? Should it print out all the elements? Should it print out all the elements with a comma separating them? Or a space? Or nothing at all? Its ambiguous what this would mean. so the default behavior for ToString unless overridden is just to tell you the type. How you print the items is left to you because there is no de facto "correct" way of outputting it.

If you want to print the items, do this

foreach(string fruit in fruits)
{
     Console.Write(fruit);
}
NotTheBatman
  • 132
  • 6
  • Hi NotTheBatman, thank you for your answer. I still get an error. So I coded this: { class Program { static void Main(string[] args) { List fruits = new List(); fruits.Add("banana"); foreach (string fruit in fruits) { Console.Write(fruit); } } } } My error is for example: namespace name 'List<>' could not be found. What does this mean. According to my study it should be correctly. – ApexLegends Jan 29 '20 at 14:36