代码检查:值元组 'GetHashCode()' 会忽略某些元素
该检查会报告在具有超过 8 个组件的值元组上调用 GetHashCode() 的情况。 对于这类元组,值元组的哈希码仅根据最后 8 个组件计算,因此第一个组件或前几个组件不会对结果产生影响。 请使用包含每个值的哈希码计算方式。
示例
using System;
public sealed class OrderKey(
int customerId,
int regionId,
int year,
int month,
int day,
int productId,
int channelId,
int campaignId,
int version)
{
public override int GetHashCode()
{
return (customerId, regionId, year, month,
day, productId, channelId,
campaignId, version).GetHashCode();
}
}
using System;
public sealed class OrderKey(
int customerId,
int regionId,
int year,
int month,
int day,
int productId,
int channelId,
int campaignId,
int version)
{
public override int GetHashCode()
{
var hashCode = new HashCode();
hashCode.Add(customerId);
hashCode.Add(regionId);
hashCode.Add(year);
hashCode.Add(month);
hashCode.Add(day);
hashCode.Add(productId);
hashCode.Add(channelId);
hashCode.Add(campaignId);
hashCode.Add(version);
return hashCode.ToHashCode();
}
}
修正方法
该检查没有专门的快速修复方法。 请将基于元组的哈希码替换为包含每个组件的显式 HashCode 计算。
2026年 7月 17日