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

推荐订阅源

AI
AI
TaoSecurity Blog
TaoSecurity Blog
H
Heimdal Security Blog
Help Net Security
Help Net Security
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Microsoft Azure Blog
Microsoft Azure Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Google DeepMind News
Google DeepMind News
爱范儿
爱范儿
The Cloudflare Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
人人都是产品经理
人人都是产品经理
大猫的无限游戏
大猫的无限游戏
N
News | PayPal Newsroom
V2EX - 技术
V2EX - 技术
博客园 - 【当耐特】
D
Darknet – Hacking Tools, Hacker News & Cyber Security
S
Secure Thoughts
C
CERT Recently Published Vulnerability Notes
罗磊的独立博客
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
P
Privacy & Cybersecurity Law Blog
有赞技术团队
有赞技术团队
S
Schneier on Security
S
SegmentFault 最新的问题
Google Online Security Blog
Google Online Security Blog
H
Hacker News: Front Page
The Last Watchdog
The Last Watchdog
Schneier on Security
Schneier on Security
PCI Perspectives
PCI Perspectives
IT之家
IT之家
Project Zero
Project Zero
博客园 - 司徒正美
P
Privacy International News Feed
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Jina AI
Jina AI
Security Latest
Security Latest
Hacker News - Newest:
Hacker News - Newest: "LLM"
腾讯CDC
C
CXSECURITY Database RSS Feed - CXSecurity.com
阮一峰的网络日志
阮一峰的网络日志
C
Check Point Blog
aimingoo的专栏
aimingoo的专栏
V
Vulnerabilities – Threatpost
W
WeLiveSecurity
NISL@THU
NISL@THU
Webroot Blog
Webroot Blog
N
Netflix TechBlog - Medium
L
Lohrmann on Cybersecurity

博客园 - 润之

关于 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;
            }
        }
    }
}