4

最初の例http://www.dotnetperls.com/convert-list-stringをメソッドに実装しようとしていますが、メソッドの2番目の引数を一致させるのに苦労しています。

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で実装されています(誰かが私に望まない限り、ここにはリストされていません)。

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

編集:「string.Join( "、"、test);」を使用してOK チェックマークを付けてこれを閉じる前に、何らかの理由で出力が機能します。

"Ilistprac.Location、Ilistprac.Location、Ilistprac.Location"

リストにあるものの代わりに何らかの理由で。

4

4 に答える 4

6

(.Net 4.0を使用しているように見えるので)まったく必要ないのでToArray()、電話をかけることができます

string.Join(",", test);
于 2012-04-11T19:24:26.257 に答える
2

かっこを付ける必要があります---()ToArray<Location>

string printitout = string.Join(",", test.Select(location => location.Site).ToArray()); 
于 2012-04-11T19:17:15.813 に答える
2

あなたのLocaionsタイプが実装する場合、あなたIEnumerableは必要ありませんToArray

string printiout = String.Join(",", test);
于 2012-04-11T19:24:10.650 に答える
1

試す:

string printitout = string.Join(",", test);
于 2012-04-11T19:16:14.460 に答える