首页 > 其他 > 详细

struct的使用场景

时间:2020-09-16 18:04:57      阅读:48      评论:0      收藏:0      [点我收藏+]

可以当做key使用,只要struct所有属性相同,就会被认为是相同值
Demo:

struct Key
    {
        public string Name { get; set; }
        public Type Type { get; set; }
    }
 private static ConcurrentDictionary<Key, string> cache = new ConcurrentDictionary<Key, string>();
        public static void Main(string[] args)
        {
            var k = new Key { Name="key1",Type=typeof(Program)};
            cache.GetOrAdd(k, key => {
                return JsonConvert.SerializeObject(key);
            });
            k = new Key { Name = "key1", Type = typeof(Program) };

            //会走缓存,因为两次k相同
            cache.GetOrAdd(k, key => {
                return JsonConvert.SerializeObject(key);
            });
      }

struct的使用场景

原文:https://www.cnblogs.com/fanfan-90/p/13680105.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!