惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

C
CXSECURITY Database RSS Feed - CXSecurity.com
Stack Overflow Blog
Stack Overflow Blog
月光博客
月光博客
T
Threat Research - Cisco Blogs
小众软件
小众软件
有赞技术团队
有赞技术团队
酷 壳 – CoolShell
酷 壳 – CoolShell
Apple Machine Learning Research
Apple Machine Learning Research
C
Cyber Attacks, Cyber Crime and Cyber Security
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
T
Tailwind CSS Blog
Cisco Talos Blog
Cisco Talos Blog
V
V2EX
博客园 - 【当耐特】
C
Cybersecurity and Infrastructure Security Agency CISA
Hugging Face - Blog
Hugging Face - Blog
The Cloudflare Blog
The Last Watchdog
The Last Watchdog
Simon Willison's Weblog
Simon Willison's Weblog
T
Threatpost
S
Secure Thoughts
O
OpenAI News
P
Proofpoint News Feed
S
SegmentFault 最新的问题
Forbes - Security
Forbes - Security
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Application and Cybersecurity Blog
Application and Cybersecurity Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Last Week in AI
Last Week in AI
宝玉的分享
宝玉的分享
Scott Helme
Scott Helme
T
Tenable Blog
A
Arctic Wolf
L
LINUX DO - 热门话题
爱范儿
爱范儿
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
www.infosecurity-magazine.com
www.infosecurity-magazine.com
V
Visual Studio Blog
Hacker News: Ask HN
Hacker News: Ask HN
Hacker News - Newest:
Hacker News - Newest: "LLM"
腾讯CDC
博客园 - Franky
WordPress大学
WordPress大学
Know Your Adversary
Know Your Adversary
博客园_首页
雷峰网
雷峰网
IT之家
IT之家
PCI Perspectives
PCI Perspectives
L
LINUX DO - 最新话题
H
Heimdal Security Blog

博客园 - 润之

关于 Excel 中导入 JSON 文件 Git Bash 在使用 pacman 安装软件包时的报错问题处理 .NET 操作 RabbitMQ 踩坑记录 Git Bash 中 pacman 安装软件包失败的问题处理 深度操作系统 deepin 20.8 定制的 SSH 安全模块问题 关于在WSL中使用RabbitMQ的问题 PowerShell脚本编写踩坑记 访问局域网资源,出现“指定的网络密码不正确” 微信图片缓存中的 dat 文件处理 关于禅道的白屏问题 Python处理编码汉字的一些方法收集 使用xrdp远程管理debian的一些记录 安利一个分支版本的Notepad2 关于 Win10 下使用 IETester 的问题 关于 Windows 10 字体安装目录的问题 关于 NPOI 导出的 Excel 出现“部分内容有问题” 的解决方法 解决美图看看不出现在“Open with”的子菜单中的问题 关于BLOB/TEXT字段存储设计及性能的简单研究 Win10 远程服务器版
匿名类对象的相关测试
润之 · 2020-04-28 · via 博客园 - 润之
HashSet<object> objs = new HashSet<object>();
objs.Add(new { a = 1, b = 2 }); // true
objs.Add(new { a = 1, b = 2 }); // false
objs.Add(new Man { id = 1, name = "A" }); // true
objs.Add(new Man { id = 1, name = "A" }); // true

Console.WriteLine(new { a = 1, b = 2 } == new { a = 1, b = 2 }); // false
Console.WriteLine(new Man { id = 1, name = "A" } == new Man { id = 1, name = "A" }); // false

Console.WriteLine(object.Equals(new { a = 1, b = 2 }, new { a = 1, b = 2 })); // true
Console.WriteLine(object.Equals(new Man { id = 1, name = "A" }, new Man { id = 1, name = "A" })); // false

Console.WriteLine(object.ReferenceEquals(new { a = 1, b = 2 }, new { a = 1, b = 2 })); // false
Console.WriteLine(object.ReferenceEquals(new Man { id = 1, name = "A" }, new Man { id = 1, name = "A" })); // false

Console.WriteLine(new { a = 1, b = 2}.GetHashCode());
Console.WriteLine(new { a = 1, b = 2}.GetHashCode()); // 内容相同的,HashCode都相同
Console.WriteLine(new { a = 1, b = 3}.GetHashCode());

Console.WriteLine(new Man { id = 1, name = "A" }.GetHashCode()); // 每次都不同
Console.WriteLine(new Man { id = 1, name = "A" }.GetHashCode()); // 每次都不同



// 结论:
//   匿名对象的 GetHashCode 和 Equals 是相同的,以便于方便地用作LINQ连接和分组中的散列键,这样的设计是合理的;
//   HashSet.Add 方法判断对象是否已经存在,是根据 GetHashCode 和 Equals 进行的,单纯的 HashCode 相同没用,还是会添加;
//   所以,下面的 DistinctBy 扩展方法 .DistinctBy(p => new { p.Id, p.Name }) 或 .DistinctBy(p => p.Id) 都可以正常工作;
//   参考[https://stackoverflow.com/questions/12123512/why-does-the-equals-implementation-for-anonymous-types-compare-fields]
//   There is also a very practical reason to do this: Anonymous types are convenient to use as hash keys in LINQ joins and groupings. 



public class Man
{
	public int id { get; set; }
	public string name { get; set; }
	
  //public override int GetHashCode()
  //{
  //  return 123;
  //}

  //public override bool Equals(object obj)
  //{
  //	return ((Man)obj).id == this.id && ((Man)obj).name == this.name;
  //}
}

public static class Extensions
{
    public static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
    {
        HashSet<TKey> seenKeys = new HashSet<TKey>();
        foreach (TSource element in source)
        {
            if (seenKeys.Add(keySelector(element)))
            {
                yield return element;
            }
        }
    }
}