可以当做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);
});
}
原文:https://www.cnblogs.com/fanfan-90/p/13680105.html