为什么我不能使用base.Select在List <string>的子类中使用Linq Select?

Mic*_*haw 2 .net c# linq

如果我实例化我的子类List<string>,那么我可以使用Linq的Select方法.

using System.Linq;

namespace LinqProblem
{
    class Program
    {
        static void Main(string[] args)
        {
            BetterList list = new BetterList();
            list.Select(l => l.ToString() == "abc");  // no compile error
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,如果我尝试在子类的定义中使用Select ...

using System.Collections.Generic;
using System.Linq;

namespace LinqProblem
{
    class BetterList : List<string>
    {
        public List<string> Stuff
        {
            get
            {
                base.Select(l => l.ToString() == "abc");  // compile error
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

错误:

' List<string>'不包含'Select'的定义.

为什么会出现这种情况,是否有解决方法?

SLa*_*aks 11

Select()是一种扩展方法.该base关键字专门用于非虚拟调用基类方法,不支持扩展方法.

将其更改为this.Select,它将正常工作.


归档时间:

查看次数:

1007 次

最近记录:

8 年,4 月 前