Unterscheidet wird weder gleich auf iequitable noch iequalityComparer .net 8 [Duplikat]C#

Ein Treffpunkt für C#-Programmierer
Guest
 Unterscheidet wird weder gleich auf iequitable noch iequalityComparer .net 8 [Duplikat]

Post by Guest »

Ich schreibe eine kleine Bibliothek, in der ich diese Klasse habe: < /p>

Code: Select all

public class ClaimPath : IEquatable
{
public IEnumerable Identifiers { get; private set; } = new List();

public bool Starred { get; private set; }

public ClaimPath(IEnumerable identifiers, bool starred)
{
// Irrelevant
}

public ClaimPath(string value)
{
// Irrelevant
}

public override bool Equals(object obj)
{
return Equals(obj as ClaimPath);
}

public bool Equals(ClaimPath other)
{
if (other == null)
return false;

return Identifiers.SequenceEqual(other.Identifiers);
}

public override int GetHashCode()
{
return HashCode.Combine(Identifiers);
}

public override string ToString()
{
var path = string.Join("/", Identifiers.Select(v => v.Value));
return Starred ? $"{path}/*" : path;
}
}

public class ClaimPathComparer : IEqualityComparer
{
public ClaimPathComparer()
{
var x = 0; // DEBUGGER HERE ACTIVATES
}

public bool Equals(ClaimPath claimPath1, ClaimPath claimPath2)
{
if (claimPath1 == null || claimPath2 == null)
return false;

return claimPath1.Equals(claimPath2);
}

public int GetHashCode([DisallowNull] ClaimPath obj)
{
return obj.GetHashCode();
}
}
< /code>
Für diese Klasse habe ich einen Test, um genau zu überprüfen PrettyPrint-Override ">[TestMethod]
public void TheTest()
{
var listOfClaimPaths = new List
{
new ClaimPath("A"),
new ClaimPath("B"),
new ClaimPath("C"),
new ClaimPath("A"),
new ClaimPath("B"),
new ClaimPath("C")
};

var distinctList = listOfClaimPaths.Distinct();
var distinctList2 = listOfClaimPaths.Distinct(new ClaimPathComparer());

Assert.IsFalse(true);
}
Bearbeiten: Das Problem mit dem unterschiedlichen Debugger entfernt. In der Lage, den Hash korrekt zu berechnen, die eindeutige Logik zu brechen. /P>

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post