标签:pre lin 区别 ble cti tab png imm yellow
var intList = new List<int>() { 1 };
var readOnlyList = new ReadOnlyCollection<int>(intList);
var immutableList = intList.ToImmutableList();
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("新增前--------------");
Console.WriteLine("ReadOnlyCollection的数量:" + readOnlyList.Count);
Console.WriteLine("ImmutableList的数量:" + immutableList.Count);
intList.Add(2);
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("新增后--------------");
Console.WriteLine("ReadOnlyCollection的数量:" + readOnlyList.Count);
Console.WriteLine("ImmutableList的数量:" + immutableList.Count);
ReadOnlyCollection只是对原对象的包装。修改原对象还是会影响当前对象。所以不是线程安全的
ImmutableList则是真正不可变的。是线程安全的
C#ImmutableList和ReadOnlyCollection的区别
标签:pre lin 区别 ble cti tab png imm yellow
原文地址:https://www.cnblogs.com/qwfy-y/p/14839056.html