Some help understanding “yield”

前端 未结 8 1169
挽巷
挽巷 2020-11-30 01:41

In my everlasting quest to suck less I\'m trying to understand the \"yield\" statement, but I keep encountering the same error.

The body of [someMetho

8条回答
  •  既然无缘
    2020-11-30 02:28

    What does the method you're using this in look like? I don't think this can be used in just a loop by itself.

    For example...

    public IEnumerable GetValues() {
        foreach(string value in someArray) {
            if (value.StartsWith("A")) { yield return value; }
        }
    }
    

提交回复
热议问题