Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linq 正在尝试字符串。加入IList并将结果输出到控制台_Linq_C# 4.0 - Fatal编程技术网

Linq 正在尝试字符串。加入IList并将结果输出到控制台

Linq 正在尝试字符串。加入IList并将结果输出到控制台,linq,c#-4.0,Linq,C# 4.0,使用“string.Join(“,”,test);”有效,但由于某些原因,我得到了以下输出: Ilistprac.Location,Ilistprac.Location,Ilistprac.Location 我尝试了ToString、Convert.ToString等,但仍然得到了输出 所有的IList接口都是用IEnurmerable实现的(除非有人想让我这样做,否则这里没有列出) 类IList2 { 静态void Main(字符串[]参数) { 字符串sSite=“test”; 字符串sBl

使用“string.Join(“,”,test);”有效,但由于某些原因,我得到了以下输出:

Ilistprac.Location,Ilistprac.Location,Ilistprac.Location

我尝试了ToString、Convert.ToString等,但仍然得到了输出

所有的IList接口都是用IEnurmerable实现的(除非有人想让我这样做,否则这里没有列出)

类IList2
{
静态void Main(字符串[]参数)
{
字符串sSite=“test”;
字符串sBldg=“test32”;
字符串sSite1=“测试”;
字符串sSite2=“测试”;
位置测试=新位置();
位置loc=新位置();
测试添加(sSite、sBldg)
测试添加(sSite1)
测试添加(sSite2)
string printitout=string.Join(,,test);//输出列表中的内容时出现问题
}
}
string printitout=string.Join(“,”,test.ToArray);
公共类位置
{
公共场所()
{
}
私有字符串_site=string.Empty;
公共字符串站点
{
获取{return\u site;}
设置{u site=value;}
}
}
公共类位置:IList
{
列表_locs=新列表();
公共位置(){}
公共无效添加(字符串sSite)
{
位置loc=新位置();
地点=苏铁矿;
位置建筑=sBldg;
_locs.Add(loc);
}
私有字符串_bldg=string.Empty;
公共建筑物
{
获取{return}
设置{u bldg=value;}
}
}

您需要为
位置
提供一个有用的
ToString
实现,因为
Join
正在为每个元素调用它。默认实现只返回类型的名称。看

所以如果你有一种

class SomeType
{
    public string FirstName { get; private set;  }
    public string LastName { get; private set; }

    public SomeType(string first, string last)
    {
        FirstName = first;
        LastName = last;
    }

    public override string ToString()
    {
        return string.Format("{0}, {1}", LastName, FirstName);
    }
}
您需要指定如何将其表示为字符串。如果这样做,可以使用string.Join生成下面的输出

var names = new List<SomeType> { 
    new SomeType("Homer", "Simpson"), 
    new SomeType("Marge", "Simpson") 
};

Console.WriteLine(string.Join("\n", names));

您需要为
Location
提供一个有用的
ToString
实现,因为
Join
正在为每个元素调用它。默认实现只返回类型的名称。看

所以如果你有一种

class SomeType
{
    public string FirstName { get; private set;  }
    public string LastName { get; private set; }

    public SomeType(string first, string last)
    {
        FirstName = first;
        LastName = last;
    }

    public override string ToString()
    {
        return string.Format("{0}, {1}", LastName, FirstName);
    }
}
您需要指定如何将其表示为字符串。如果这样做,可以使用string.Join生成下面的输出

var names = new List<SomeType> { 
    new SomeType("Homer", "Simpson"), 
    new SomeType("Marge", "Simpson") 
};

Console.WriteLine(string.Join("\n", names));

如果要保持当前方法,您必须重写
ToString()
inc您的
Location
类以提供一些有意义的输出,例如:

public override string ToString()
{
    return Site;
} 

如果要保持当前方法,您必须重写
ToString()
inc您的
Location
类以提供一些有意义的输出,例如:

public override string ToString()
{
    return Site;
} 

我明白了,要返回多个值,它看起来需要格式化,谢谢!它并不是真的返回多个值。它是关于如何将您的类型表示为
字符串
Join
对序列中的
Location
的每个实例调用
ToString
。类的默认字符串表示形式是类型名。如果您还需要为类型重写
ToString
。明白了,我不知道默认实现是什么,需要重写才能将其转换为字符串。我在返回这些值时遇到了一些问题,但是让它们像trickI看到的那样格式化,要返回多个值,看起来需要格式化,谢谢!它并不是真的返回多个值。它是关于如何将您的类型表示为
字符串
Join
对序列中的
Location
的每个实例调用
ToString
。类的默认字符串表示形式是类型名。如果您还需要为类型重写
ToString
。明白了,我不知道默认实现是什么,需要重写才能将其转换为字符串。我在返回这些值时遇到了一些问题,但是将它们格式化成这样就可以了