English 中文(简体)
是否有可能在一个类别中实施若干可计量的<T>?
原标题:Is it possible to implement several IEnumerable<T> in one class?

我想做的是:

class T : IEnumerable<string>, IEnumerable<int>
{
    string [] _strings = new string [20];
    int[] _ints = new int[20];

    public T() { }

    IEnumerator<string> IEnumerable<string>.GetEnumerator()
    {
        foreach (string str in _strings)
            yield return str;
    }

    IEnumerator<int> IEnumerable<int>.GetEnumerator()
    {
        foreach (int i in _ints)
            yield return i;
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return this.GetEnumerator();
    }

}
//Using in code:
T t = new T();
foreach (int i in t)
   //to do something
foreach (string str in t)
   //to do stuff

我愿知道,有没有办法实现这一目标。 是否有trick?

最佳回答

你们几乎都设法做到了 落实<>> > 两个接口——你刚刚需要改变非遗传性执行,以显示你再次试图将哪些一般性执行交给他。 例如:

IEnumerator IEnumerable.GetEnumerator()
{
    return ((IEnumerable<int>)this).GetEnumerator();
}

但是,由于你重新实施一个以上<代码>。 IE amountable<T> 接口你需要在每一座椅子中投放,显示使用:

T t = new T();
foreach (int i in (IEnumerable<int>) t)
   //to do something
foreach (string str in (IEnumerable<string>) t)

如果可能的话,我会强烈建议against这样做,这给阅读你的法典的人造成了许多混乱。

See section 8.8.4 of the C# Language specification for details of the composeder treatments the expression to iterate over in a foreach loop.

(用“热”接口执行one,由您提供某种“过失”——但我不认为会真正使之更好。)

问题回答

我建议界定一些结构类型,其唯一领域是你类别的例子,最初是图形构造者,其唯一公共成员是GetE数字仪,并且采用E 数字和斜体;以你的基型号使用适当的方法。 之后,您的原产阶级会执行一个成员,该成员会再次出现一个适当的障碍。

For example, if your class Foo had a GetStringEnumerator method which returned a type that implements IEnumerable<String>, you could have Foo.AsStrings return a struct FooStringEnumerable whose GetEnumerator method called Foo.GetStringEnumerator.

Note that by having your enumerable thing be a struct rather than a class, you would avoid having to create an extra object instance in the common case where it is used in a "for-each" loop, since both vb and c# would duck-type the GetEnumerator method instead of casting to IEnumerable or IEnumerable<T>.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签