尝试串入IList

nha*_*hat 3 linq interface c#-4.0

我正在尝试将第一个示例http://www.dotnetperls.com/convert-list-string实施到我的方法中,但是我很难匹配该方法的第二个参数:

string printitout = string.Join(",", test.ToArray<Location>);
Run Code Online (Sandbox Code Playgroud)

错误信息:

The best overloaded method match for 'string.Join(string,
System.Collections.Generic.IEnumerable<string>)' has some invalid arguments
Run Code Online (Sandbox Code Playgroud)

所有的IList接口也都使用IEnurmerable实现(除非有人希望我在这里未列出)。

class IList2
{
    static void Main(string[] args)
    {

     string sSite = "test";
     string sSite1 = "test";
     string sSite2 = "test";

     Locations test = new Locations();
     Location loc = new Location();
     test.Add(sSite)
     test.Add(sSite1)
     test.Add(sSite2)
     string printitout = string.Join(",", test.ToArray<Location>); //having issues calling what it needs.

     }
 }
string printitout = string.Join(",", test.ToArray<Location>);


public class Location
{
    public Location()
    {

    }
    private string _site = string.Empty;
    public string Site
    {
        get { return _site; }
        set { _site = value; }
    }
}

public class Locations : IList<Location>
{
    List<Location> _locs = new List<Location>();

    public Locations() { }

    public void Add(string sSite)
    {
        Location loc = new Location();
        loc.Site = sSite;
        _locs.Add(loc);
    }
 }
Run Code Online (Sandbox Code Playgroud)

编辑:可以使用“ string.Join(”,“,test);” 工作,在我用对号将其关闭之前,由于某种原因,我的输出会输出:

“ Ilistprac.Location,Ilistprac.Location,Ilistprac.Location”

由于某种原因而不是列表中的内容。

dle*_*lev 5

您根本不需要ToArray()(因为您似乎正在使用.Net 4.0),因此可以拨打电话

string.Join(",", test);
Run Code Online (Sandbox Code Playgroud)