string[]在哪里继承的IEnumerable?

忆水思寒 2019-07-18 02:21:17
string[] arrStr = { "a", "b", "c" };
return arrStr.Count(p=>p==str);

string[]类型能使用count,是应为它继承了IEnumerable<T>接口(没找到),然后System.Linq中的Enumerable类对这个接口进行了扩展有了count函数,所以就能调用了
问题,为什么找不到那个泛型接口(我是在Array类中找的,string[]毕竟是个数组类型),或者这句话是错的(它不用继承IEnumerable<T>接口也能调用linq中的扩展方法(肯定错的))
如果你找到了哪里继承的,那贴出代码或截图
如果那句话是错的,那也告诉我哪里错了,正确的是什么样的



...全文
212 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
angel6709 2019-07-24
  • 打赏
  • 举报
回复
兄弟,我只是给你举了一个例子,说白了,Array不需要实现IEnumerable接口,只有GetEnumerator()方法就可以了。 GetEnumerator(),同样也可以是扩展方法(上面的例子不是)。
引用 12 楼 忆水思寒 的回复:
[quote=引用 10 楼 angel6709 的回复:] GetEnumerator足矣, 你可以实现自己的枚举类,例如:

  public static class MyStringExt
    {
        public static int Count(this MyString thiz, Func<char, bool> fun)
        {
            int count = 0;
            foreach (var c in thiz)
            {
                if (fun(c)){ count++; } 
            }
            return count;
        }
    }
    public class MyString
    {
        private string _value;
        public MyString(string value)
        {
            _value = value;
        }
        public IEnumerator<char> GetEnumerator()
        {
            if (_value == null) { throw new Exception("没有枚举集合"); }
            foreach (var c in _value)
            {
                yield return c;
            }
        }

        static void Main(string[] args)
        {
            MyString str =new MyString("hello");
            Console.WriteLine(str.Count(a => a == 'l')) ;
        } 
    }
大哥思路很好,但源码中linq是通过扩展IEnumerable<T>接口达到调用的,不是类;那针对题目的问题也解答下吧[/quote]
忆水思寒 2019-07-23
  • 打赏
  • 举报
回复
引用 10 楼 angel6709 的回复:
GetEnumerator足矣, 你可以实现自己的枚举类,例如:

  public static class MyStringExt
    {
        public static int Count(this MyString thiz, Func<char, bool> fun)
        {
            int count = 0;
            foreach (var c in thiz)
            {
                if (fun(c)){ count++; } 
            }
            return count;
        }
    }
    public class MyString
    {
        private string _value;
        public MyString(string value)
        {
            _value = value;
        }
        public IEnumerator<char> GetEnumerator()
        {
            if (_value == null) { throw new Exception("没有枚举集合"); }
            foreach (var c in _value)
            {
                yield return c;
            }
        }

        static void Main(string[] args)
        {
            MyString str =new MyString("hello");
            Console.WriteLine(str.Count(a => a == 'l')) ;
        } 
    }
大哥思路很好,但源码中linq是通过扩展IEnumerable<T>接口达到调用的,不是类;那针对题目的问题也解答下吧
忆水思寒 2019-07-23
  • 打赏
  • 举报
回复
引用 9 楼 正怒月神 的回复:
[quote=引用 8 楼 忆水思寒 的回复:] 这就是在Array类中看不到 IEnumerable<T>接口 的原因?
Array是一个基类,一个抽象类。 int[],string[],float[]................ 是他的派生类。 人家派生类在实现额外的接口,有什么不可以的吗? 你光看Array的继承和接口,看不到IList<T>等等也很正常吧。 [/quote] 大哥,能贴个图或代码吗
正怒月神 2019-07-22
  • 打赏
  • 举报
回复
引用 6 楼 忆水思寒 的回复:
[quote=引用 2 楼 正怒月神 的回复:] 大哥。。。。我上个贴已经发了图了。。。 你对准Count一个f12过去,就能看到,是扩展方法。。。 这个不是继承了。。。。
现在不是扩展方法那个问题了,count中的扩展方法也只是对IEnumerable<T>接口进行的扩展,也只有IEnumerable<T>接口才能对此方法进行调用,现在是你string[]看不到有继承IEnumerable<T>接口的地方,居然也能够调用,这就是问题,你好好看看,也看下 #2的帖子[/quote]
忆水思寒 2019-07-22
  • 打赏
  • 举报
回复
引用 2 楼 正怒月神 的回复:
大哥。。。。我上个贴已经发了图了。。。
你对准Count一个f12过去,就能看到,是扩展方法。。。
这个不是继承了。。。。

现在不是扩展方法那个问题了,count中的扩展方法也只是对IEnumerable<T>接口进行的扩展,也只有IEnumerable<T>接口才能对此方法进行调用,现在是你string[]看不到有继承IEnumerable<T>接口的地方,居然也能够调用,这就是问题,你好好看看,也看下 #2的帖子
忆水思寒 2019-07-22
  • 打赏
  • 举报
回复
引用 2 楼 正怒月神 的回复:
大哥。。。。我上个贴已经发了图了。。。
你对准Count一个f12过去,就能看到,是扩展方法。。。
这个不是继承了。。。。

大哥是我表达有问题吗?你仔细看下问题,我问的是 string[]类型在哪里继承的IEnumerable<T>接口,不是count函数怎么实现的,那个我不关心
angel6709 2019-07-22
  • 打赏
  • 举报
回复
GetEnumerator足矣, 你可以实现自己的枚举类,例如:

  public static class MyStringExt
    {
        public static int Count(this MyString thiz, Func<char, bool> fun)
        {
            int count = 0;
            foreach (var c in thiz)
            {
                if (fun(c)){ count++; } 
            }
            return count;
        }
    }
    public class MyString
    {
        private string _value;
        public MyString(string value)
        {
            _value = value;
        }
        public IEnumerator<char> GetEnumerator()
        {
            if (_value == null) { throw new Exception("没有枚举集合"); }
            foreach (var c in _value)
            {
                yield return c;
            }
        }

        static void Main(string[] args)
        {
            MyString str =new MyString("hello");
            Console.WriteLine(str.Count(a => a == 'l')) ;
        } 
    }
正怒月神 2019-07-22
  • 打赏
  • 举报
回复
引用 8 楼 忆水思寒 的回复:

这就是在Array类中看不到 IEnumerable<T>接口 的原因?

Array是一个基类,一个抽象类。
int[],string[],float[]................
是他的派生类。
人家派生类在实现额外的接口,有什么不可以的吗?
你光看Array的继承和接口,看不到IList<T>等等也很正常吧。

忆水思寒 2019-07-22
  • 打赏
  • 举报
回复
引用 7 楼 正怒月神 的回复:
[quote=引用 6 楼 忆水思寒 的回复:] [quote=引用 2 楼 正怒月神 的回复:] 大哥。。。。我上个贴已经发了图了。。。 你对准Count一个f12过去,就能看到,是扩展方法。。。 这个不是继承了。。。。
现在不是扩展方法那个问题了,count中的扩展方法也只是对IEnumerable<T>接口进行的扩展,也只有IEnumerable<T>接口才能对此方法进行调用,现在是你string[]看不到有继承IEnumerable<T>接口的地方,居然也能够调用,这就是问题,你好好看看,也看下 #2的帖子[/quote] [/quote] 这就是在Array类中看不到 IEnumerable<T>接口 的原因?
stherix 2019-07-18
  • 打赏
  • 举报
回复
string[]就是泛型数组,是实现了IEumerable<>的,
正怒月神 2019-07-18
  • 打赏
  • 举报
回复
这个Count的扩展方法本质是个泛型 Count<T> 但是编译器帮你推断了是string类型, 所以你写成了 Count(x=>.....) 但是你看图就知道
正怒月神 2019-07-18
  • 打赏
  • 举报
回复
大哥。。。。我上个贴已经发了图了。。。 你对准Count一个f12过去,就能看到,是扩展方法。。。 这个不是继承了。。。。
忆水思寒 2019-07-18
  • 打赏
  • 举报
回复
建议回答之后,看下这个C之IEnumerable

110,566

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧