C# 集合【与java不同的地方】

▲字典赋值(【添加】),两种方式:out引用需要赋值给原引用才会修改。

  1. sort[5] = "c"; 
  2. sort.Add(4, "d");会报错,

//取值和更改值都可以使用[]

 

【删除】Remove
【修改】sort[5] = "c";

▲覆盖字典中的key,使用索引器,不能使用Add()。
RankDic.Add(1, fashionRankList);有问题,不会覆盖,会报错,
已修改为self.Top50[totalArea] = RankDic;
使用索引器获取值,没有key值会报异常sort[6]

 

 

 ▲交集:Intersect() 差集:Except(),并集:Union()

字典根据值排序。根据键都可以使用OrderBy排序。
sort.OrderBy(item => item.Value).ToDictionary(x => x.Key, y => y.Value);
多级排序还可使用.ThenBy。
▲HashSet<>一般存储基本类型,

例如id

存储对象可能不会覆盖,因为对象内存地址不一样就不会覆盖,即使id一样也不覆盖

例如ET框架中Entity中存储组件的集合一个HashSet字段,一个Dictionary字段,存储的数据是一样的,只是一个有Bson序列化【HashSet】,一个没有序列化【Dictionary】,HashSet不能保证存储的组件不重复,Dictionary来保证。

[BsonElement("C")]
[BsonIgnoreIfNull]
private HashSet<Component> components = new HashSet<Component>();

[BsonIgnore]
private Dictionary<Type, Component> componentDict = new Dictionary<Type, Component>();

 

posted @ 2020-05-14 21:11  好Wu赖  阅读(249)  评论(0编辑  收藏  举报