Need help print list of strings

好久不见. 提交于 2020-01-17 07:50: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:


"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);
}


来源:https://stackoverflow.com/questions/27238568/need-help-print-list-of-strings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!