简体   繁体   English

在IEnumerable的情况下使用System.Linq <t> ?

[英]Use of System.Linq in case of IEnumerable<t>?

I have an API which returns System.Collections.Generic.IEnumerable<t> , if I import( using ) System.Linq . 如果我using System.Linq导入( using ),则我有一个API会返回System.Collections.Generic.IEnumerable<t> I am able to do Count() / ToList() on the returned object. 我可以对返回的对象执行Count() / ToList()

What would be the relation between those two( System.Collections.Generic , System.Linq ), packages ? 这两个( System.Collections.GenericSystem.Linq )包之间的关系是什么?

Thanks Pavan 谢谢帕万

IEnumerable<> is that relationship. IEnumerable <>是这种关系。 Those methods (Count(), ToList()) are extension methods on IEnumerable<> 这些方法(Count(),ToList())是IEnumerable <>上的扩展方法

This means that anything that implements IEnumerable (IList, ICollection, Arrays) they all will have those methods. 这意味着任何实现IEnumerable的东西(IList,ICollection,Arrays)都将具有这些方法。 To use them you need to "import" those methods with Using System.Linq; 要使用它们,您需要使用Using System.Linq“导入”这些方法。 statement 声明

A simple extension method on String. 一个简单的String扩展方法。 can be

public static class StringExtensions 
{
    public static string CountWords(this string input )
    {
         return imnput.Split(new char[]{' '}).Length;
    }
}

System.Linq命名空间中有一个静态类Enumerable ,它提供了与IEnumerable<T>类型一起使用的大多数扩展方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM