Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.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/2/cmake/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
C# 在派生列表的类中<>;,如何访问列表_C#_.net - Fatal编程技术网

C# 在派生列表的类中<>;,如何访问列表

C# 在派生列表的类中<>;,如何访问列表,c#,.net,C#,.net,这个问题搞砸了 见底 在结果B中,如何访问列表 public class ResultsB : List<ResultB> { public string Methods { get { // what I want is return string.Join(", ", this); // and have this be List<ResultB>

这个问题搞砸了
见底

在结果B中,如何访问列表

public class ResultsB : List<ResultB>
{
    public string Methods
    {
        get
        {
            // what I want is 
            return string.Join(", ", this);
            // and have this be List<ResultB> 
            // this fails
            // it returns "PuzzleBinarySearchWeighted.Program+ResultB, PuzzleBinarySearchWeighted.Program+ResultB" 
            // I just get information about the object 
            // this fails also - same thing information about the object 
            //return string.Join(", ", this.GetEnumerator());
        }
    }
    public void testEnum()
    {  
        // this works
        foreach (ResultB resultB in this)
        {
            Debug.WriteLine(resultB.MethodsString);
        }
    }
}
我只是看错了
我需要一个来自所有列表的iEnumerable
我无法删除,因为答案的票数已满
对不起,我是职业训练局的,要求你也这样做

public string Methods
{
    get
    {
        return string.Join(", ", this.MethodsAll);
    }
}
public HashSet<string> MethodsAll
{
    get
    {
        HashSet<string> hs = new HashSet<string>();
        foreach (ResultB resultB in this)
        {
            foreach (string s in resultB.Methods)
            {
                hs.Add(s);
            }
        }
        return hs;
    }
}
公共字符串方法
{
收到
{
返回字符串.Join(“,”,this.MethodsAll);
}
}
公共哈希集方法
{
收到
{
HashSet hs=新的HashSet();
foreach(此中的ResultB ResultB)
{
foreach(resultB.Methods中的字符串s)
{
hs.添加(s);
}
}
返回hs;
}
}

隐式地使用

例如,调用
Add
,如下所示
Add(“hello world”)
,它隐式地是
this.Add(“hello world”)

OP想要使用
字符串。直接加入

OP编辑了问题,并表示希望使用
string.Join
,如下所示:

string.Join(“,”,this)

您可以使用的
string.Join
重载是,它将对给定序列中找到的每个对象调用
Object.ToString
(即
IEnumerable
)。因此,您需要在结果类中提供一个重写的
Object.ToString
方法(您可以在上进行检查):


…您的代码将按预期工作

您使用
base
关键字。为什么要从
列表中派生?你应该更喜欢组合而不是继承。另外,你的帖子上写着“实现”,但是
列表
是一个类,而不是一个接口。请定义失败。对此进行详细说明,提供例外情况。。。stacktrace@MatíasFidemrAzer将更新“它返回”拼图BinarySearchWeighted.Program+ResultB。。。“-你希望它会返回什么?您正在尝试将对象列表作为字符串联接。此问题与您从
列表中导出的事实无关。请查看我的修订版。我在一个字符串中访问哪个。加入。@Papazzi Now?:D@Paparazzi看我重新编辑的答案,让我知道它是否已经解决了你的问题。我的问题现在一团糟。让我们试着把它弄干净。@狗仔队好的,同时,我要洗盘子了,哈哈
public string Methods
{
    get
    {
        return string.Join(", ", this.MethodsAll);
    }
}
public HashSet<string> MethodsAll
{
    get
    {
        HashSet<string> hs = new HashSet<string>();
        foreach (ResultB resultB in this)
        {
            foreach (string s in resultB.Methods)
            {
                hs.Add(s);
            }
        }
        return hs;
    }
}
public class CustomListOfStrings : List<string>
{
      // ...
      private void DoStuff() 
      {
            Add("Whatever");
      }
}
foreach(ResultB result in this)
{

}
public class ResultB
{
     public override string ToString()
     {
           // You need to provide what would be a string representation
           // of ResultB
           return "Who knows what you want to return here";
     }
}