user1993843
user1993843

Reputation: 155

How to return contents of a List<char>

 public List<char> Columns(String input)
    {
        char[] temp = input.ToCharArray();
        List<char> columns = new List<char>(input.Length);
        for (int i = 0; i < input.Length; i++)
        {
            if (other.Contains(temp[i]))
            {
                columns.Add((char)(i+1));
            }
        }
        return columns;
    }

My goal here was to have this method return a List. However, when I went to output the contents in another method I got 'System.Collections.Generic.List[char]

Console.WriteLine(ex.Columns(line));

That was my attempt to output it.

Upvotes: 0

Views: 12098

Answers (4)

Roman Royter
Roman Royter

Reputation: 1665

First, let's answer the question of why you get System.Collections.Generic.List[char].

The method you are using to output something to the console is Console.WriteLine. If you go to the linked page you will see many overloads of that method, all taking different kinds of parameters. Let's think which one is being used when you pass an argument of type List<char>.

If you look carefully in that list, you will see that there is no overload that takes List<anything>. As you know, in .NET every type derives from System.Object, or in other words, every type is a kind of System.Object. When the C# compiler tries to choose a method overload, and it fails to resolve the exact type, it reverts to look for is a kind of relationship. In our case, the only overload that matches is the one that takes System.Object.

So what does this overload do? If you read the description you will easily figure it out.

If value is null, only the line terminator is written. Otherwise, the ToString method of value is called to produce its string representation, and the resulting string is written to the standard output stream.

Okay, so when you pass your list of characters, it will call a ToString() method on it. Let's take a look at the ToString() method on the List<T>. When you click on the "ToString" link you will see that you end up on System.Object.ToString() page. This means that the List type doesn't override the base method. So what does the Object.ToString() do?

The default implementation of the ToString method returns the fully qualified name of the type of the Object, as the following example shows.

Mystery solved!

When you write Console.WriteLine(ex.Columns(line)); the compiler invokes the Console.WriteLine(Object) overload which calls Object.ToString() on the passed parameter, which prints the fully qualified name of the type.

Armed with this knowledge, you can figure out what to do in order to print the characters themselves. For example you can pass an array of characters to invoke the Console.WriteLine(Char[]) overload by writing Console.WriteLine(ex.Columns(line).ToArray());

Upvotes: 4

Joel Coehoorn
Joel Coehoorn

Reputation: 415735

Try this:

Console.WriteLine(new string(ex.Columns(line).ToArray()));

Upvotes: 0

shf301
shf301

Reputation: 31394

No an array would not help you. To output the contents of a collection you need to loop through each element in the collected.

Using list.ToString() calls the ToString() on on List<T> which calls the standard object ToString implementation , which is to print out the type's name, which is what you are seeing.

You must iterate over it and access each element instead:

foreach(var item in list) {
    string s = item.ToString();
}

Upvotes: 0

Richard Schneider
Richard Schneider

Reputation: 35477

Not sure what you are trying to accomplish here. A string can always be indexed to get a specific character.

So why introduce a List and why have a funky method. Just do:

s[i]

Upvotes: 0

Related Questions