Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

What is: System.Collections.Generic.List`1[System.String]

5,495 views
Skip to first unread message

Al Murphy

unread,
Jan 27, 2012, 11:07:13 AM1/27/12
to
Hi everyone,

Can you help me please? My questions is, is it normal to see
"System.Collections.Generic.List`1[System.String]"? Can I get rid of
it in some way?

I wrote the followign code where it is occuring:

**** BEGIN STRANGE CODE ****

public Dictionary<string, object> PostParameters { get; set; }

public AddToDictionary(string job, List<string> taskList)
{

PostParameters.Add("meta_Data1", job);
PostParameters.Add("meta_Data2", taskList );

}

**** END STRANGE CODE ****


When I mouse over this second onject that I add to the dictionary
object during debeugging or view it using fiddler I see that it says
something like:


[1] = {[meta_Data2,
System.Collections.Generic.List`1[System.String]]}


The value under this is perfect but I don't like the
"System.Collections.Generic.List`1[System.String]" bit. It looks a bit
ugly IMHO. The problem only occurs when I add the List<string> object
to the dictionary?

Does anyone know how to make this a bit tidier? I would appreciate
any comments/suggestions ideas or guidance that you may like to share.

Thank you,
Al.

Peter Duniho

unread,
Jan 27, 2012, 12:11:13 PM1/27/12
to
On Fri, 27 Jan 2012 08:07:13 -0800 (PST), Al Murphy wrote:

> [...]
> [1] = {[meta_Data2,
> System.Collections.Generic.List`1[System.String]]}
>
>
> The value under this is perfect but I don't like the
> "System.Collections.Generic.List`1[System.String]" bit. It looks a bit
> ugly IMHO. The problem only occurs when I add the List<string> object
> to the dictionary?
>
> Does anyone know how to make this a bit tidier? I would appreciate
> any comments/suggestions ideas or guidance that you may like to share.

There's nothing to fix. That is just what an instance of List<string>
returns from the ToString() method. It shows the type name, plus the type
parameter name. It's mainly just a debugging convenience (ordinarily, no
one would ever call that method or display the results to a user).

Pete

Andy

unread,
Jan 27, 2012, 12:18:53 PM1/27/12
to
You've defined your list as being able to hold only string objects.
When you try to pass something to this list that isn't a string
object, .NET will automatically invoke the .ToString() method on your
non-string object to turn it into a string. The results of this are
different based on what the object originally was. If it was a
number, then ToString will convert it to a string literal of that
number. But, on complex compound objects ToString() will usually
return the spelling of the typedefinition used to create that object.


Peter Duniho

unread,
Jan 27, 2012, 1:26:16 PM1/27/12
to
On Fri, 27 Jan 2012 09:18:53 -0800 (PST), Andy wrote:

> You've defined your list as being able to hold only string objects.
> When you try to pass something to this list that isn't a string
> object, .NET will automatically invoke the .ToString() method on your
> non-string object to turn it into a string. [...]

This is not true at all. The point of generics, and it will apply to
List<string> as well, is that at _compile-time_ you will be prohibited from
using objects not already typed as the type used for the generic.

This means that it will be a compiler error to attempt to add anything not
already typed as "string" to the List<string>, and you also will not be
able to assign members of the List<string> to anything except a variable of
type "string".

No automatic conversion will occur.

In addition: even if the "automatic conversion" was true, it wouldn't apply
here. The OP is looking at the List<string> instance itself, not objects
contained by that instance.

Pete
0 new messages