C#集合之不变的集合

  如果对象可以改变其状态,就很难在多个同时运行的任务中使用。这些集合必须同步。如果对象不能改变器状态,就很容易在多个线程中使用。
  Microsoft提供了一个新的集合库:Microsoft Immutable Collection。顾名思义,它包含不变的集合类————创建后不能改变的集合类。该类在System.Collection.Immutable中定义。

            //使用静态的Create方法创建该数组,Create方法被重载,可以传递任意数量的元素
          ImmutableArray<string> a1 = ImmutableArray.Create<string>();
          //Add 方法不会改变不变集合本身,而是返回一个新的不变集合
          ImmutableArray<string> a2 = a1.Add("Williams");
          //可以一次调用多个Add方法
          ImmutableArray<string> a3 = a2.Add("Ferrari").Add("Mercedes").Add("Red Bull Racing");
          foreach (var item in a3)
          {
            Console.WriteLine(item);
          }

 

  在使用不变数组的每个阶段,都没有复制完整的集合。相反,不变类型使用了共享状态,仅在需要时复制集合。

  但是,先填充集合,再将它变成不变的数组会更高效(使用ToImmutableList方法)。需要进行一些处理时,可以再变为可变的集合(使用ToBuilder方法)。使用不变集合的提供的构建器ImmutableList<Account>.Builder。

List<Account> accounts = new List<Account>() {
        new Account {
          Name = "Scrooge McDuck", 
          Amount = 667377678765m
        },
        new Account {
          Name = "Donald Duck",
          Amount = -200m
        },
       new Account {
         Name = "Ludwig von Drake",
         Amount = 20000m
        }};
      ImmutableList<Account> immutableAccounts = accounts.ToImmutableList();

      ImmutableList<Account>.Builder builder = immutableAccounts.ToBuilder();
      for (int i = 0; i < builder.Count; i++)
      {
        Account a = builder[i];
        if (a.Amount > 0)
        {
          builder.Remove(a);
        }
      }

      ImmutableList<Account> overdrawnAccounts = builder.ToImmutable();


      foreach (var item in overdrawnAccounts)
      {
        Console.WriteLine("{0} {1}", item.Name, item.Amount);
      }
      
      public class Account
      {
        public string Name { get; set; }
        public decimal Amount { get; set; }
      }

 

只读集合(http://www.cnblogs.com/afei-24/p/6824791.html)提供了集合的只读视图。在不使用只读视图访问集合的情况下,该集合仍可以修改。而永远不能改变不变的集合。

posted @ 2017-05-10 14:54  Ruby_Lu  阅读(1872)  评论(0编辑  收藏  举报