我有一个PagedModel类,它实现IEnumerable只返回ModelData,忽略分页数据.我还重写了Equals和GetHashCode,以允许比较两个PagedModel对象的ModelData,PageNumber和TotalPages以及PageSize.
这是问题所在
Dim p1 As New PagedModel() With {
.PageNumber = 1,
.PageSize = 10,
.TotalPages = 10,
.ModelData = GetModelData()
}
Dim p2 As New PagedModel() With {
.PageNumber = 1,
.PageSize = 10,
.TotalPages = 10,
.ModelData = GetModelData()
}
p1.Equals(p2) =====> True
Assert.AreEqual(p1, p2) ======> False!
Run Code Online (Sandbox Code Playgroud)
看起来NUnit调用它的内部EnumerableEqual方法来比较我的PagedModel而不是使用我提供的Equals方法!有没有办法覆盖这种行为,或者我是否必须编写自定义断言.
将现有的.NET 3.5应用程序移植到.NET 4.0有些麻烦.该守则不是我自己编写的,所以我不知道为什么事情就像它们一样.
这是情况:如果从Visual Studio启动应用程序(版本或调试模式无关紧要),并且如果应用程序从Debug-folder启动,则代码工作正常问题是Release-deploy,因为它不是'自4.0(以及4.5)以来运作良好: - /
这是最初的电话:
someObject.Text = Elements.GetElement(Int16.Parse(cb1.Text));
Run Code Online (Sandbox Code Playgroud)
这是代码:
public class Elements : EnumBase<int, Elements>
{
public static readonly Elements Element1 = Create("Number 0", 0);
public static readonly Elements Element2 = Create("Number 1", 1);
private static Elements Create(string text, int value)
{
return new Elements() { text = text, value = value };
}
public static String GetElement(int id)
{
// The Following Code safes the day and let the release deploy work fine.
// It doesn´t …Run Code Online (Sandbox Code Playgroud) 在C#中,如果在覆盖Equals()时未能覆盖GetHashCode(),那么具体可能出现什么问题?
使用NHibernate时,为什么在实体中应该覆盖Equals或GetHashCode?在哪些情况下这些原因有效?
可以在网上找到的一些原因:
还有其他原因吗?
我有一个类型,我在IDictionary中使用它作为键.类型如下
public class Employee
{
public string Name { get; set; }
public int ID { get; set; }
public override bool Equals(object obj)
{
Employee emp = obj as Employee;
if (emp != null)
return emp.Name.Equals(this.Name);
return false;
}
public override int GetHashCode()
{
return this.Name.GetHashCode();
}
}
Run Code Online (Sandbox Code Playgroud)
现在我已经在我的主要内容中创建了一个字典,如下所示
IDictionary<Employee, int> empCollection = new Dictionary<Employee, int>();
Employee emp1 = new Employee() { Name = "abhi", ID = 1 };
Employee emp2 = new Employee() { Name = "vikram", ID …Run Code Online (Sandbox Code Playgroud) 我有一个案例,我需要抓取一堆不同的项目,但我的源是一个具有两个属性的对象的集合,如下所示:
public class SkillRequirement
{
public string Skill { get; set; }
public string Requirement { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我尝试获得如下集合:
SkillRequirementComparer sCom = new SkillRequirementComparer();
var distinct_list = source.Distinct(sCom);
Run Code Online (Sandbox Code Playgroud)
我试图为此实现一个IEqualityComparer<T>,但我对这个GetHashCode()方法感到难过.
Comparer的类:
public class SkillRequirementComparer : IEqualityComparer<SkillRequirement>
{
public bool Equals(SkillRequirement x, SkillRequirement y)
{
if (x.Skill.Equals(y.Skill) && x.Requirement.Equals(y.Requirement))
{
return true;
}
else
{
return false;
}
}
public int GetHashCode(SkillRequirement obj)
{
//?????
}
}
Run Code Online (Sandbox Code Playgroud)
通常我会GetHashCode()在一个属性上使用,但因为我在两个属性上进行比较,所以我有点不知所措.我做错了什么,或者遗漏了一些非常明显的东西?
对于实现大量的IEqualityComparers有些懒惰,并且考虑到我无法轻松编辑被比较对象的类实现,我使用了以下内容,意味着与Distinct()和Except()扩展方法一起使用.:
public class GenericEqualityComparer<T> : IEqualityComparer<T>
{
Func<T, T, bool> compareFunction;
Func<T, int> hashFunction;
public GenericEqualityComparer(Func<T, T, bool> compareFunction, Func<T, int> hashFunction)
{
this.compareFunction = compareFunction;
this.hashFunction = hashFunction;
}
public bool Equals(T x, T y)
{
return compareFunction(x, y);
}
public int GetHashCode(T obj)
{
return hashFunction(obj);
}
}
Run Code Online (Sandbox Code Playgroud)
看起来不错,但每次真的需要一个哈希函数?我知道哈希码用于将对象放入存储桶中.不同的桶,对象不相等,并且不调用相等.
如果GetHashCode返回相同的值,则调用equals.(来自:为什么在重写Equals方法时重写GetHashCode很重要?)
那么可能出现什么问题,例如(我听到很多程序员惊恐地尖叫),GetHashCode返回一个常量,强制调用Equal?
HashSet<T>.Add首先比较结果GetHashCode.如果它们是相同的,它会调用Equals.
现在,我的理解是为了实现GetHashCode,必须用对象的字段来完成某些事情.一个简单的示例实现可以在被覆盖的System.Object.GetHashCode的最佳算法是什么?.
在我的测试中,在填充随机数据的1.000.000对对象上进行比较,两者之间的性能或多或少相等.GetHashCode在链接示例中实现,Equals只需调用Equals所有字段.那么为什么要用GetHashCode它Equals?
我正在尝试使用list.Unionin 合并两个列表,LinqPad但我无法让它工作,并想检查我的理解是否正确.
鉴于这个简单的类:
public class Test
{
public int Id { get; set;}
public int field1 { get; set; }
public bool Equals(Test other)
{
return this.Id.Equals(other.Id);
}
}
Run Code Online (Sandbox Code Playgroud)
两个列表填充如下:
List<Test> list = new List<Test>();
list.Add( new Test { Id = 1, field1 = 1});
list.Add( new Test { Id = 1, field1 = 2});
list.Add( new Test { Id = 2, field1 = 3});
list.Add( new Test { Id = 2, field1 = 4});
List<Test> …Run Code Online (Sandbox Code Playgroud) 我需要能够在 C# 中搜索大约 200 万个项目的集合。应该可以在多个字段上进行搜索。简单的字符串匹配就足够了。
使用像数据库这样的外部依赖不是一种选择,但使用内存数据库就可以了。
主要目标是做到这一点内存高效。
集合中的类型非常简单,没有长字符串:
public class Item
{
public string Name { get; set; } // Around 50 chars
public string Category { get; set; } // Around 20 chars
public bool IsActive { get; set; }
public DateTimeOffset CreatedAt { get; set; }
public IReadOnlyList<string> Tags { get; set; } // 2-3 items
}
Run Code Online (Sandbox Code Playgroud)
明确重点和要求:
使用简单List<T> …
c# ×7
.net ×4
linq ×3
gethashcode ×2
performance ×2
.net-4.0 ×1
assert ×1
collections ×1
distinct ×1
enums ×1
equals ×1
ienumerable ×1
in-memory ×1
nhibernate ×1
nunit ×1
optimization ×1
release ×1