尝试string.Join一个IList [英] Trying to string.Join an IList

查看:118
本文介绍了尝试string.Join一个IList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to implement the first example http://www.dotnetperls.com/convert-list-string into my method but I'm having a hard time matching the 2nd argument for the method:

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

错误消息:

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

所有IList接口都是用IEnurmerable实现的(除非有人要我,否则不在此处列出)。

All the IList interfaces are implemented with the IEnurmerable too (just not listed here unless someone wants me to).

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

编辑:
好​​用字符串。加入(,,测试);工作,在我用复选标记关闭之前,由于某种原因我的输出,输出:

Ok using "string.Join(",", test);" works, before I close this with a check mark, for some reason my output, outputs:

Ilistprac.Location,Ilistprac.Location,Ilistprac.Location

"Ilistprac.Location, Ilistprac.Location, Ilistprac.Location"

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

for some reason instead of what's in the list.

推荐答案

你不喜欢t需要 ToArray()(因为看起来你正在使用.Net 4.0)所以你可以拨打电话

You don't need ToArray() at all (since it appears you're using .Net 4.0) so you can make the call

string.Join(",", test);

这篇关于尝试string.Join一个IList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆