Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/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# - Fatal编程技术网

C# 将扩展方法添加到列表中

C# 将扩展方法添加到列表中,c#,C#,这是我的密码 class Student { some code } static class Filter { static void TypeFilter(this List<Student> result, string type) { result = result.FindAll(x=>x.type == type); } } 班级学生 { 一些代码 } 静态类过滤器 { 静态void类型筛选器(此列表结果,字符串

这是我的密码

class Student
{
    some code 
}

static class Filter
{
    static void TypeFilter(this List<Student> result, string type)
    {
        result = result.FindAll(x=>x.type == type);
    }
}
班级学生
{
一些代码
}
静态类过滤器
{
静态void类型筛选器(此列表结果,字符串类型)
{
result=result.FindAll(x=>x.type==type);
}
}
当我使用这样的扩展方法时

    List<Student> a = some code;
    a.TypeFilter("someType");
列表a=一些代码;
a、 类型过滤器(“someType”);

列表a未被筛选,列表应为引用类型,那么为什么a没有更改,我做错了什么吗?

您不能以这种方式分配它尝试以下操作:

static List<Student> TypeFilter(this List<Student> result, string type)
    {
       return result.FindAll(x=>x.type == type);
    }
静态列表类型筛选器(此列表结果,字符串类型)
{
返回result.FindAll(x=>x.type==type);
}
像这样使用它:

List<Student> a = some code;
List<Student> filteredStudentList = a.TypeFilter("someType");
列表a=一些代码;
List filteredStudentList=a.TypeFilter(“someType”);

您可以使用
RemoveAll

static void TypeFilter(this List<Student> result, string type)
{
    result.RemoveAll(x=>x.type != type);
}
静态void类型过滤器(此列表结果,字符串类型)
{
result.RemoveAll(x=>x.type!=type);
}

A是引用类型,但在调用FindAll时创建了一个新列表。FindAll是一个返回新列表的函数。它相当于愚弄法:

List<Student> FindAll (List<Student> students, string filter){
    List<Student> newList = new List<Student>();

    foreach(var student in students){
        if(filter == student.type)
            newList.Add(student);
    }
    return newList;
}
List FindAll(列出学生,字符串过滤器){
List newList=新列表();
foreach(学生中的var学生){
if(filter==student.type)
newList.Add(学生);
}
返回newList;
}
如果要使用返回值,则需要通过创建变量捕获对返回值的引用:


var filteredStudents=students.TypeFilter(“someFilter”)

在引用方法中,您不能将
指针指定给它,并且任何带有参数的方法在未将其设置为
ref
参数的情况下都不能将其指定给它,因此,您的代码生成的
列表
不能按您描述的方式指定给
结果

由于它是一个
列表
,因此您可以迭代并删除项目,而不是替换指针

static void TypeFilter(this List<Student> result, string type)
{
    foreach(var s in result
        .Except(result.FindAll(x => x.type == type))
        .ToArray())  // VERY Important - modifying a list while iterating through it will throw an exception!
    { result.remove(s); }
}
静态void类型过滤器(此列表结果,字符串类型)
{
foreach(var s在结果中)
.Except(result.FindAll(x=>x.type==type))
.ToArray()//非常重要-在遍历列表时修改列表将引发异常!
{result.remove(s);}
}

以下是您的原因;我们没有看到结果:

static void TypeFilter(this List<Student> result, string type)
{
    result = result.FindAll(x=>x.type == type);
}

List<Student> a = some code;
a.TypeFilter("someType");
其用法是:

List<Student> a = some code;
a = a.TypeFilter("someType");
列表a=一些代码;
a=a.TypeFilter(“someType”);

您必须重新分配它。它不会修改您原来的收藏,而是创建一个新的收藏。@Jeroenvanevel我想您的意思是“您重新分配它”,而不是“您必须重新分配它”。
TypeFilter
中的重新分配正是造成此问题的原因。@hvd:对,也是这样。我指的是调用代码,其中只应用了
a.TypeFilter()
,但没有重新分配给
a
。为什么要重新发明轮子?对于这样的事情,有
LINQ2Objects
@jeroenvanevel啊,好吧,我不认为这是一个问题,我认为
TypeFilter
的要点应该是它修改了列表,所以调用方代码不必使用任何赋值。但你的方法也会奏效。
List<Student> a = some code;
a = a.TypeFilter("someType");