开发者

ASP.NET - Static Variables & State Machines -Will one user affect another?

I have implemented some functionality in C# using the yield statement with the function returning an IEnumerable.

My question is that if this function is a static function in a static class, does it implicitly hold anything as a state machine that would affect more than one user or does the iterator hold everythi开发者_JAVA技巧ng relating to the state? (Static members are application wide.)


No it does not. The created IEnumerable<T> will be of a type which contains no additional static data. It is possible for it to reference static information but only if you explicitly access it from within the iterator

For example.

public static class Utils {
  public static IEnumerable<int> Range(int start, int count) {
    for (var i = start; i <= count; i++) {
      yield return i;
    }
  }
}

Roughly translates into

public static class Utils {
  private class RangeIterator : IEnumerable<int>, IEnumerator<int> {
    ... 
    // iterator state machine
  }
  public static IEnumerable<int> Range(int start, int count) {
    return new RangeIterator(start, count);
  }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜