Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

In the How Can I Expose Only a Fragment of IList<> question one of the answers had the following code snippet:

(在“ 如何仅显示IList <>的片段”问题中,答案之一具有以下代码片段:)

IEnumerable<object> FilteredList()
{
    foreach(object item in FullList)
    {
        if(IsItemInPartialList(item))
            yield return item;
    }
}

What does the yield keyword do there?

(yield关键字在那里做什么?)

I've seen it referenced in a couple places, and one other question, but I haven't quite figured out what it actually does.

(我已经在几个地方提到过它,还有另一个问题,但是我还没有弄清楚它的实际作用。)

I'm used to thinking of yield in the sense of one thread yielding to another, but that doesn't seem relevant here.

(我习惯于从一个线程向另一个线程屈服的角度考虑yield,但这在这里似乎无关紧要。)

  ask by Herms translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
264 views
Welcome To Ask or Share your Answers For Others

1 Answer

The yield keyword actually does quite a lot here.

(yield关键字实际上在这里做了很多工作。)

The function returns an object that implements the IEnumerable<object> interface.

(该函数返回一个实现IEnumerable<object>接口的IEnumerable<object> 。)

If a calling function starts foreach ing over this object, the function is called again until it "yields".

(如果调用函数开始foreach此对象,则会再次调用该函数,直到“屈服”为止。)

This is syntactic sugar introduced in C# 2.0 .

(这是C#2.0中引入的语法糖。)

In earlier versions you had to create your own IEnumerable and IEnumerator objects to do stuff like this.

(在早期版本中,您必须创建自己的IEnumerableIEnumerator对象才能执行此类操作。)

The easiest way understand code like this is to type-in an example, set some breakpoints and see what happens.

(理解这样的代码最简单的方法是键入示例,设置一些断点,然后看看会发生什么。)

Try stepping through this example:

(尝试逐步执行此示例:)

public void Consumer()
{
    foreach(int i in Integers())
    {
        Console.WriteLine(i.ToString());
    }
}

public IEnumerable<int> Integers()
{
    yield return 1;
    yield return 2;
    yield return 4;
    yield return 8;
    yield return 16;
    yield return 16777216;
}

When you step through the example, you'll find the first call to Integers() returns 1 .

(在逐步浏览示例时,您会发现对Integers()的第一次调用返回1 。)

The second call returns 2 and the line yield return 1 is not executed again.

(第二次调用返回2 ,并且行yield return 1不再执行。)

Here is a real-life example:

(这是一个真实的例子:)

public IEnumerable<T> Read<T>(string sql, Func<IDataReader, T> make, params object[] parms)
{
    using (var connection = CreateConnection())
    {
        using (var command = CreateCommand(CommandType.Text, sql, connection, parms))
        {
            command.CommandTimeout = dataBaseSettings.ReadCommandTimeout;
            using (var reader = command.ExecuteReader())
            {
                while (reader.Read())
                {
                    yield return make(reader);
                }
            }
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...