扩展在C#中枚举类? [英] Extending the Enumerable class in c#?

查看:453
本文介绍了扩展在C#中枚举类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的情况在C#中扩展枚举类补充说,长期接受的参数范围的新方法。我不能这样定义

I have situation to extend the Enumerable class in c# to add the new Range method that accepts long parameters. I cannot define the method like this

public static IEnumerable<long> Range(this Enumerable source, long start, long length)
{
    for (long i = start; i < length; i++)
    {
        yield return i;
    }
}



由于扩展方法只能通过它的对象入店。它给我一个错误

Since extension methods are accesible only through its objects. And it gives me an error

System.Linq.Enumerable:静态类型
不能用作参数

'System.Linq.Enumerable': static types cannot be used as parameters

能否someonce澄清我如何做到这一点。

Can someonce clarify me how to do this

注:我知道我们可以很容易地解决这个问题没有扩展方法,但我需要这个Enumrable类。

Note: I know we can easily solve this without extension methods, but i needed this Enumrable class.

推荐答案

您(像我)正在寻找静态扩展方法:

You (like me) are looking for static extension methods:

http://madprops.org/blog/static-extension-methods/

这是不可能在C#中。最近的另一种方法是定义另一个静态类具有类似名称(LongEnumerable?),并添加您的静态方法来表示。

It's not possible in C#. The closest alternative is to define another static class with a similar name (LongEnumerable?) and add your static method to that.

这篇关于扩展在C#中枚举类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆