Need help print list of strings

2019-08-30 18:10发布

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());

1条回答
Summer. ? 凉城
2楼-- · 2019-08-30 18:42

"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);
}
查看更多
登录 后发表回答