xRay
xRay

Reputation: 11

Need help print list of strings

i am trying to print all innertext values for class xyz, but this is all i get printed "System.Collections.Generic.List`1[System.String]

    public List<String> getL1Names()
    {

        UITestControl document = browinX.CurrentDocumentWindow;
        HtmlControl control = new HtmlControl(document);
        control.SearchProperties.Add(HtmlControl.PropertyNames.Class, "xyz");
        UITestControlCollection controlcollection = control.FindMatchingControls();
        List<string> names = new List<string>();
        foreach (HtmlControl link in controlcollection)
        {
            if (link is HtmlHyperlink)
            names.Add(control.InnerText);
        }
        return names;
    }

using this to print

Console.WriteLine(siteHome.getL1Names());

Upvotes: 0

Views: 1276

Answers (2)

Paritosh
Paritosh

Reputation: 83

You print list of string like this:

string stringList = string.Join(",", siteHome.getL1Names().Select(x => x));
Console.WriteLine(stringList);

Upvotes: 1

Eric J.
Eric J.

Reputation: 150208

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

That is because System.Collections.Generic.List<T> does not overload ToString(). The default implementation (inherited from System.Object) prints the name of the object's type, which is what you are seeing.

You probably mean to iterate through all of the elements in the list, and print each separately.

You can change

Console.WriteLine(siteHome.getL1Names());

to something like

foreach (var name in siteHome.getL1Names()) 
{
    Console.WriteLine(name);
}

Upvotes: 1

Related Questions