概要:不变的集合
1.价值:在多线程中处理数据就很方便,因为不便,不需要特殊的同步工作。
2.原理:并不是真的不可变,而是每次有变化都生成一个新的集合,类似于string.
3.效率技巧:并不是真的一有变化就生成新的对象,在使用不变集合的每一个阶段,都没有复制完整的集合,而是使用了共享状态,仅在需要时复制集合。
代码
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Collections.Immutable;
namespace ConsoleApp11
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
ImmutableArray<string> vs = ImmutableArray.Create<string>();
ImmutableArray<string> vs1 = vs.Add("one");
ImmutableArray<string> vs2 = vs1.Add("two").Add("three");
foreach (string i in vs2) {
Console.WriteLine(i);
}
Console.ReadKey();
}
}
}
运行结果