How to use a custom comparer for EqualityComparer.Default?How to use the IEqualityComparerHow do you give a C# Auto-Property a default value?How do I enumerate an enum in C#?Why we need the IEqualityComparer,IEqualityComparer<T> interface?Result of calling IEquatable<T>.Equals(T obj) when this == null and obj == null?Not-hash-based set collection for storing unique objects with custom equality comparer - C#What is the difference between using IEqualityComparer and Equals/GethashCode Override?add Equality Comparer class to base class for custom property classes in c#C# Errors when doing a simple datarow comparer64bit HashCodes, IEqualityComparer & Intersect/ExceptEqualityComparer<T>.Default doesn't return the derived EqualityComparer
How to optimize IN query on indexed column
Inadvertently nuked my disk permission structure - why?
Closet Wall, is it Load Bearing?
What does Kasparov mean here?
Spacing setting of math mode
How were the LM astronauts supported during the moon landing and ascent? What were the max G's on them during these phases?
Why are so many countries still in the Commonwealth?
Using "Kollege" as "university friend"?
Terence Tao - type books in other fields?
What exactly makes a General Products hull nearly indestructible?
how to add 1 milliseconds on a datetime string?
How can I receive packages while in France?
What does the minus sign mean in measurements in datasheet footprint drawings?
What do teaching faculty do during semester breaks?
Does static fire reduce reliability?
Very basic singly linked list
No-cloning theorem does not seem precise
Would it be a good idea to memorize relative interval positions on guitar?
Why are there not any MRI machines available in Interstellar?
Area of parallelogram = Area of square. Shear transform
Why do websites not use the HaveIBeenPwned API to warn users about exposed passwords?
Why no ";" after "do" in sh loops?
How can I create a shape in Illustrator which follows a path in descending order size?
Moving files accidentally to an not existing directory erases files?
How to use a custom comparer for EqualityComparer.Default?
How to use the IEqualityComparerHow do you give a C# Auto-Property a default value?How do I enumerate an enum in C#?Why we need the IEqualityComparer,IEqualityComparer<T> interface?Result of calling IEquatable<T>.Equals(T obj) when this == null and obj == null?Not-hash-based set collection for storing unique objects with custom equality comparer - C#What is the difference between using IEqualityComparer and Equals/GethashCode Override?add Equality Comparer class to base class for custom property classes in c#C# Errors when doing a simple datarow comparer64bit HashCodes, IEqualityComparer & Intersect/ExceptEqualityComparer<T>.Default doesn't return the derived EqualityComparer
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
Note: my case is for byte[] but I believe a good answer would work for any type.
Visual Studio's auto-generated implementation of Equals
uses EqualityComparer<T>.Default.Equals(T x, T y)
for reference types. I have a lot of classes with byte arrays that needs to be included in Equals
so I'd like to keep Visual studio's code if possible but Default
returns a ObjectEqualityComparer
for byte arrays. I've written a simple byte array comparer but I'm not sure how to proceed to have it used instead of ObjectEqualityComparer
.
public class Foo
public int Id get;set;
public byte[] Data get;set;
public override bool Equals(object obj)
var foo = obj as Foo;
return foo != null &&
Id == foo.Id &&
EqualityComparer<byte[]>.Default.Equals(Data, foo.Data);
static void Main
Foo f1 = new Foo Id = 1, Data = new byte[1] 0xFF ;
Foo f2 = new Foo Id = 1, Data = new byte[1] 0xFF ;
bool result = f1.Equals(f2); // false
public class ByteArrayComparer
public bool Equals(byte[] x, byte[] y)
return x.SequenceEqual(y);
public int GetHashCode(byte[] obj)
return obj.GetHashCode();
// as Servy said, this is wrong but it's not the point of the question,
// assume some working implementation
Should ByteArrayComparer implement IEqualityComparer, inherit from EqualityComparer and override the methods, or something else?
c# iequalitycomparer
|
show 6 more comments
Note: my case is for byte[] but I believe a good answer would work for any type.
Visual Studio's auto-generated implementation of Equals
uses EqualityComparer<T>.Default.Equals(T x, T y)
for reference types. I have a lot of classes with byte arrays that needs to be included in Equals
so I'd like to keep Visual studio's code if possible but Default
returns a ObjectEqualityComparer
for byte arrays. I've written a simple byte array comparer but I'm not sure how to proceed to have it used instead of ObjectEqualityComparer
.
public class Foo
public int Id get;set;
public byte[] Data get;set;
public override bool Equals(object obj)
var foo = obj as Foo;
return foo != null &&
Id == foo.Id &&
EqualityComparer<byte[]>.Default.Equals(Data, foo.Data);
static void Main
Foo f1 = new Foo Id = 1, Data = new byte[1] 0xFF ;
Foo f2 = new Foo Id = 1, Data = new byte[1] 0xFF ;
bool result = f1.Equals(f2); // false
public class ByteArrayComparer
public bool Equals(byte[] x, byte[] y)
return x.SequenceEqual(y);
public int GetHashCode(byte[] obj)
return obj.GetHashCode();
// as Servy said, this is wrong but it's not the point of the question,
// assume some working implementation
Should ByteArrayComparer implement IEqualityComparer, inherit from EqualityComparer and override the methods, or something else?
c# iequalitycomparer
1
There is no way to getEqualityComparer<byte[]>.Default
to return anything else no matter what you write. It'd be quite the global problem if you could -- whose "global default equality comparer forbyte[]
" is the right one? A custom type can implementIEquatable
, but for the existing system types you don't get to assign new, "more sensible" defaults. The best you can do is create new comparers, and use those explicitly.
– Jeroen Mostert
Mar 26 at 16:02
1
Your comparer doesn't return the same hash code for "equal" arrays, so it doesn't even work. Be thankful you aren't able to make that the default comparer.
– Servy
Mar 26 at 16:05
@JeroenMostert Right, I figured it wasn't possible.
– 0xFF
Mar 26 at 16:07
At least it should implementIEqualityComparer<byte[]>
to use it e.g. in linq methods, but you will not be able to letEqualityComparer<byte[]>.Default
return an instance of your class.
– René Vogt
Mar 26 at 16:07
1
@Servy: thanks, you have eloquently elaborated the purpose of my quotes around "correct".
– Jeroen Mostert
Mar 26 at 16:22
|
show 6 more comments
Note: my case is for byte[] but I believe a good answer would work for any type.
Visual Studio's auto-generated implementation of Equals
uses EqualityComparer<T>.Default.Equals(T x, T y)
for reference types. I have a lot of classes with byte arrays that needs to be included in Equals
so I'd like to keep Visual studio's code if possible but Default
returns a ObjectEqualityComparer
for byte arrays. I've written a simple byte array comparer but I'm not sure how to proceed to have it used instead of ObjectEqualityComparer
.
public class Foo
public int Id get;set;
public byte[] Data get;set;
public override bool Equals(object obj)
var foo = obj as Foo;
return foo != null &&
Id == foo.Id &&
EqualityComparer<byte[]>.Default.Equals(Data, foo.Data);
static void Main
Foo f1 = new Foo Id = 1, Data = new byte[1] 0xFF ;
Foo f2 = new Foo Id = 1, Data = new byte[1] 0xFF ;
bool result = f1.Equals(f2); // false
public class ByteArrayComparer
public bool Equals(byte[] x, byte[] y)
return x.SequenceEqual(y);
public int GetHashCode(byte[] obj)
return obj.GetHashCode();
// as Servy said, this is wrong but it's not the point of the question,
// assume some working implementation
Should ByteArrayComparer implement IEqualityComparer, inherit from EqualityComparer and override the methods, or something else?
c# iequalitycomparer
Note: my case is for byte[] but I believe a good answer would work for any type.
Visual Studio's auto-generated implementation of Equals
uses EqualityComparer<T>.Default.Equals(T x, T y)
for reference types. I have a lot of classes with byte arrays that needs to be included in Equals
so I'd like to keep Visual studio's code if possible but Default
returns a ObjectEqualityComparer
for byte arrays. I've written a simple byte array comparer but I'm not sure how to proceed to have it used instead of ObjectEqualityComparer
.
public class Foo
public int Id get;set;
public byte[] Data get;set;
public override bool Equals(object obj)
var foo = obj as Foo;
return foo != null &&
Id == foo.Id &&
EqualityComparer<byte[]>.Default.Equals(Data, foo.Data);
static void Main
Foo f1 = new Foo Id = 1, Data = new byte[1] 0xFF ;
Foo f2 = new Foo Id = 1, Data = new byte[1] 0xFF ;
bool result = f1.Equals(f2); // false
public class ByteArrayComparer
public bool Equals(byte[] x, byte[] y)
return x.SequenceEqual(y);
public int GetHashCode(byte[] obj)
return obj.GetHashCode();
// as Servy said, this is wrong but it's not the point of the question,
// assume some working implementation
Should ByteArrayComparer implement IEqualityComparer, inherit from EqualityComparer and override the methods, or something else?
c# iequalitycomparer
c# iequalitycomparer
edited Mar 26 at 16:10
0xFF
asked Mar 26 at 15:59
0xFF0xFF
5881 gold badge8 silver badges27 bronze badges
5881 gold badge8 silver badges27 bronze badges
1
There is no way to getEqualityComparer<byte[]>.Default
to return anything else no matter what you write. It'd be quite the global problem if you could -- whose "global default equality comparer forbyte[]
" is the right one? A custom type can implementIEquatable
, but for the existing system types you don't get to assign new, "more sensible" defaults. The best you can do is create new comparers, and use those explicitly.
– Jeroen Mostert
Mar 26 at 16:02
1
Your comparer doesn't return the same hash code for "equal" arrays, so it doesn't even work. Be thankful you aren't able to make that the default comparer.
– Servy
Mar 26 at 16:05
@JeroenMostert Right, I figured it wasn't possible.
– 0xFF
Mar 26 at 16:07
At least it should implementIEqualityComparer<byte[]>
to use it e.g. in linq methods, but you will not be able to letEqualityComparer<byte[]>.Default
return an instance of your class.
– René Vogt
Mar 26 at 16:07
1
@Servy: thanks, you have eloquently elaborated the purpose of my quotes around "correct".
– Jeroen Mostert
Mar 26 at 16:22
|
show 6 more comments
1
There is no way to getEqualityComparer<byte[]>.Default
to return anything else no matter what you write. It'd be quite the global problem if you could -- whose "global default equality comparer forbyte[]
" is the right one? A custom type can implementIEquatable
, but for the existing system types you don't get to assign new, "more sensible" defaults. The best you can do is create new comparers, and use those explicitly.
– Jeroen Mostert
Mar 26 at 16:02
1
Your comparer doesn't return the same hash code for "equal" arrays, so it doesn't even work. Be thankful you aren't able to make that the default comparer.
– Servy
Mar 26 at 16:05
@JeroenMostert Right, I figured it wasn't possible.
– 0xFF
Mar 26 at 16:07
At least it should implementIEqualityComparer<byte[]>
to use it e.g. in linq methods, but you will not be able to letEqualityComparer<byte[]>.Default
return an instance of your class.
– René Vogt
Mar 26 at 16:07
1
@Servy: thanks, you have eloquently elaborated the purpose of my quotes around "correct".
– Jeroen Mostert
Mar 26 at 16:22
1
1
There is no way to get
EqualityComparer<byte[]>.Default
to return anything else no matter what you write. It'd be quite the global problem if you could -- whose "global default equality comparer for byte[]
" is the right one? A custom type can implement IEquatable
, but for the existing system types you don't get to assign new, "more sensible" defaults. The best you can do is create new comparers, and use those explicitly.– Jeroen Mostert
Mar 26 at 16:02
There is no way to get
EqualityComparer<byte[]>.Default
to return anything else no matter what you write. It'd be quite the global problem if you could -- whose "global default equality comparer for byte[]
" is the right one? A custom type can implement IEquatable
, but for the existing system types you don't get to assign new, "more sensible" defaults. The best you can do is create new comparers, and use those explicitly.– Jeroen Mostert
Mar 26 at 16:02
1
1
Your comparer doesn't return the same hash code for "equal" arrays, so it doesn't even work. Be thankful you aren't able to make that the default comparer.
– Servy
Mar 26 at 16:05
Your comparer doesn't return the same hash code for "equal" arrays, so it doesn't even work. Be thankful you aren't able to make that the default comparer.
– Servy
Mar 26 at 16:05
@JeroenMostert Right, I figured it wasn't possible.
– 0xFF
Mar 26 at 16:07
@JeroenMostert Right, I figured it wasn't possible.
– 0xFF
Mar 26 at 16:07
At least it should implement
IEqualityComparer<byte[]>
to use it e.g. in linq methods, but you will not be able to let EqualityComparer<byte[]>.Default
return an instance of your class.– René Vogt
Mar 26 at 16:07
At least it should implement
IEqualityComparer<byte[]>
to use it e.g. in linq methods, but you will not be able to let EqualityComparer<byte[]>.Default
return an instance of your class.– René Vogt
Mar 26 at 16:07
1
1
@Servy: thanks, you have eloquently elaborated the purpose of my quotes around "correct".
– Jeroen Mostert
Mar 26 at 16:22
@Servy: thanks, you have eloquently elaborated the purpose of my quotes around "correct".
– Jeroen Mostert
Mar 26 at 16:22
|
show 6 more comments
1 Answer
1
active
oldest
votes
Create and use an instance of your custom comparer instead of using EqualityComparer<byte[]>.Default
in your class:
public class Foo
public int Id get; set;
public byte[] Data get; set;
private readonly ByteArrayComparer _comparer = new ByteArrayComparer();
public override bool Equals(object obj)
var foo = obj as Foo;
return foo != null &&
Id == foo.Id &&
_comparer.Equals(Data, foo.Data);
You may also want to implement IEqualityComparer<T>
and GetHashCode()
in your ByteArrayComparer
class. EqualityComparer<T>.Default
returns an instance of a class that implements this interface, but I assume you don't want to use this one as you have implemented your own custom comparer.
How to use the IEqualityComparer
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55361430%2fhow-to-use-a-custom-comparer-for-equalitycomparert-default%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Create and use an instance of your custom comparer instead of using EqualityComparer<byte[]>.Default
in your class:
public class Foo
public int Id get; set;
public byte[] Data get; set;
private readonly ByteArrayComparer _comparer = new ByteArrayComparer();
public override bool Equals(object obj)
var foo = obj as Foo;
return foo != null &&
Id == foo.Id &&
_comparer.Equals(Data, foo.Data);
You may also want to implement IEqualityComparer<T>
and GetHashCode()
in your ByteArrayComparer
class. EqualityComparer<T>.Default
returns an instance of a class that implements this interface, but I assume you don't want to use this one as you have implemented your own custom comparer.
How to use the IEqualityComparer
add a comment |
Create and use an instance of your custom comparer instead of using EqualityComparer<byte[]>.Default
in your class:
public class Foo
public int Id get; set;
public byte[] Data get; set;
private readonly ByteArrayComparer _comparer = new ByteArrayComparer();
public override bool Equals(object obj)
var foo = obj as Foo;
return foo != null &&
Id == foo.Id &&
_comparer.Equals(Data, foo.Data);
You may also want to implement IEqualityComparer<T>
and GetHashCode()
in your ByteArrayComparer
class. EqualityComparer<T>.Default
returns an instance of a class that implements this interface, but I assume you don't want to use this one as you have implemented your own custom comparer.
How to use the IEqualityComparer
add a comment |
Create and use an instance of your custom comparer instead of using EqualityComparer<byte[]>.Default
in your class:
public class Foo
public int Id get; set;
public byte[] Data get; set;
private readonly ByteArrayComparer _comparer = new ByteArrayComparer();
public override bool Equals(object obj)
var foo = obj as Foo;
return foo != null &&
Id == foo.Id &&
_comparer.Equals(Data, foo.Data);
You may also want to implement IEqualityComparer<T>
and GetHashCode()
in your ByteArrayComparer
class. EqualityComparer<T>.Default
returns an instance of a class that implements this interface, but I assume you don't want to use this one as you have implemented your own custom comparer.
How to use the IEqualityComparer
Create and use an instance of your custom comparer instead of using EqualityComparer<byte[]>.Default
in your class:
public class Foo
public int Id get; set;
public byte[] Data get; set;
private readonly ByteArrayComparer _comparer = new ByteArrayComparer();
public override bool Equals(object obj)
var foo = obj as Foo;
return foo != null &&
Id == foo.Id &&
_comparer.Equals(Data, foo.Data);
You may also want to implement IEqualityComparer<T>
and GetHashCode()
in your ByteArrayComparer
class. EqualityComparer<T>.Default
returns an instance of a class that implements this interface, but I assume you don't want to use this one as you have implemented your own custom comparer.
How to use the IEqualityComparer
edited Mar 27 at 14:50
answered Mar 26 at 16:01
mm8mm8
97.3k9 gold badges19 silver badges34 bronze badges
97.3k9 gold badges19 silver badges34 bronze badges
add a comment |
add a comment |
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55361430%2fhow-to-use-a-custom-comparer-for-equalitycomparert-default%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
There is no way to get
EqualityComparer<byte[]>.Default
to return anything else no matter what you write. It'd be quite the global problem if you could -- whose "global default equality comparer forbyte[]
" is the right one? A custom type can implementIEquatable
, but for the existing system types you don't get to assign new, "more sensible" defaults. The best you can do is create new comparers, and use those explicitly.– Jeroen Mostert
Mar 26 at 16:02
1
Your comparer doesn't return the same hash code for "equal" arrays, so it doesn't even work. Be thankful you aren't able to make that the default comparer.
– Servy
Mar 26 at 16:05
@JeroenMostert Right, I figured it wasn't possible.
– 0xFF
Mar 26 at 16:07
At least it should implement
IEqualityComparer<byte[]>
to use it e.g. in linq methods, but you will not be able to letEqualityComparer<byte[]>.Default
return an instance of your class.– René Vogt
Mar 26 at 16:07
1
@Servy: thanks, you have eloquently elaborated the purpose of my quotes around "correct".
– Jeroen Mostert
Mar 26 at 16:22