Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to string.Join an IList

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

error message:

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

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

Edit: 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"

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

like image 425
nhat Avatar asked Dec 20 '22 23:12

nhat


1 Answers

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);
like image 189
dlev Avatar answered Jan 18 '23 05:01

dlev