有什么具体类型是'收益回报“回报? [英] What concrete type does 'yield return' return?

查看:175
本文介绍了有什么具体类型是'收益回报“回报?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是具体类型此的IEnumerable<串>

 私人的IEnumerable<串GT; GetIEnumerable()
{
收益率回归A;
收益率回归A;
收益率回归A;
}


解决方案

这是一个编译器生成的类型。编译器生成一个的IEnumerator<串> 实现,返回三个一的价值观和的IEnumerable<串> skeleton类它提供的这些在其的GetEnumerator 方法之一。



生成的代码看起来的的东西的像这样*:

  //不知道什么生成的类的命名约定是 -  
//这真的只是在黑暗中拍摄。
类GetIEnumerable_Enumerator:IEnumerator的<串GT;
{
INT _state;
串_current;

公共BOOL的MoveNext()
{
开关(_state ++)
{
案0:
_current =一个;
中断;
壳体1:
_current =一个;
中断;
壳2:
_current =一个;
中断;
默认:
返回FALSE;
}

返回真;
}

公共字符串当前
{
{返回_current; }
}

对象IEnumerator.Current
{
{返回电流; }
}

无效IEnumerator.Reset()
{
//不知道这一个 - 从未真正尝试过......
/ /我只是猜测
_state = 0;
_current = NULL;
}
}

类GetIEnumerable_Enumerable:IEnumerable的<串GT;
{
公众的IEnumerator<串GT;的GetEnumerator()
{
返回新GetIEnumerable_Enumerator();
}

的IEnumerator IEnumerable.GetEnumerator()
{
返回的GetEnumerator();
}
}



或许,作为SLaks说,在他的回答中,两种实现在同一个类中结束。我写了这基于我的生成的代码我已经看着面前的波涛汹涌内存;真的,一个班就足够了,因为没有理由上述功能需要两个。



其实,细想起来,这两种实现真正的的落在一个类中,因为我只记得使用收益语句必须具备的或者的<$ C $的返回类型的功能C>的IEnumerable< T> 的IEnumerator< T>


$ b $ :b

不管怎样,我就让你执行代码更正了我精神上发布



<子> *这纯粹是用于说明目的;我并没有为它的实际精度。这只是一般性地展示了编译器如何做它做什么,根据我在我自己的调查看到的证据。


What is the concrete type for this IEnumerable<string>?

private IEnumerable<string> GetIEnumerable()
{
    yield return "a";
    yield return "a";
    yield return "a";
}

解决方案

It's a compiler-generated type. The compiler generates an IEnumerator<string> implementation that returns three "a" values and an IEnumerable<string> skeleton class that provides one of these in its GetEnumerator method.

The generated code looks something like this*:

// No idea what the naming convention for the generated class is --
// this is really just a shot in the dark.
class GetIEnumerable_Enumerator : IEnumerator<string>
{
    int _state;
    string _current;

    public bool MoveNext()
    {
        switch (_state++)
        {
            case 0:
                _current = "a";
                break;
            case 1:
                _current = "a";
                break;
            case 2:
                _current = "a";
                break;
            default:
                return false;
        }

        return true;
    }

    public string Current
    {
        get { return _current; }
    }

    object IEnumerator.Current
    {
        get { return Current; }
    }

    void IEnumerator.Reset()
    {
        // not sure about this one -- never really tried it...
        // I'll just guess
        _state = 0;
        _current = null;
    }
}

class GetIEnumerable_Enumerable : IEnumerable<string>
{
    public IEnumerator<string> GetEnumerator()
    {
        return new GetIEnumerable_Enumerator();
    }

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

Or maybe, as SLaks says in his answer, the two implementations end up in the same class. I wrote this based on my choppy memory of generated code I'd looked at before; really, one class would suffice, as there's no reason the above functionality requires two.

In fact, come to think of it, the two implementations really should fall within a single class, as I just remembered the functions that use yield statements must have a return type of either IEnumerable<T> or IEnumerator<T>.

Anyway, I'll let you perform the code corrections to what I posted mentally.

*This is purely for illustration purposes; I make no claim as to its real accuracy. It's only to demonstrate in a general way how the compiler does what it does, based on the evidence I've seen in my own investigations.

这篇关于有什么具体类型是'收益回报“回报?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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