【问题标题】:ASP.NET - Static Variables & State Machines -Will one user affect another?ASP.NET - 静态变量和状态机 - 一个用户会影响另一个用户吗?
【发布时间】:2011-07-30 08:02:21
【问题描述】:

我在 C# 中使用 yield 语句实现了一些功能,该函数返回一个 IEnumerable。

我的问题是,如果这个函数是静态类中的一个静态函数,它是否隐含地持有任何会影响多个用户的状态机,或者迭代器是否持有与所有相关的东西国家? (静态成员是应用程序范围内的。)

【问题讨论】:

    标签: c# asp.net iterator yield-return


    【解决方案1】:

    不,它没有。创建的IEnumerable<T> 将属于不包含其他静态数据的类型。它可以引用静态信息,但前提是您从迭代器中显式访问它

    例如。

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

    大致翻译成

    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);
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2018-03-15
      • 1970-01-01
      • 1970-01-01
      • 2021-03-21
      • 1970-01-01
      • 1970-01-01
      • 2016-10-18
      • 2015-10-04
      • 1970-01-01
      相关资源
      最近更新 更多