c# - How do you override operator == when using interfaces instead of actual types? -
I have some code:
How should I implement operator == so that it can be said Could that be the variable interface of IMyClass?
Public Class MyClass: IMyClass {Public Stable Bull Operator == (MyClass a, MyClass b) {if (ReferenceEquals (a, b)) back true; If (Object) A == blank ((object) B == empty) return false; Description is false; Public stationary BULL operator! = (Myclass A, Micalus B) {return! (A == B);} } Class program {static zero main (string [] args) {IMyClass m1 = New MyClass (); IMyClass m2 = New MyClass (); MyClass M3 = New MyClass (); MyClass M4 = New MyClass (); Console. The light line (M1 == M2); // does not go into the custom == function, why not? Console.lightline (m3 == m4); // custom == go to function}}
The key is that you '
for operator No operator defined = (IMIClass X, IMIClass Y)
so can not say anything to the compiler
operator == (MyClass x, MyClass y)
Because it does not know that
m1
andm2
actuallyMyClass
.As far as I know the operator using the interface Programming is no way to apply - after all, many implementations, can potentially provide a possible one of your own to the point of opacity.
Personally, I am trying to talk about equality on non-seal types - equality and legacy does not blend very well, it doubles for the interface, of course: ) You can get an appropriate
IEqualityComparer & lt; IMyClass & gt;
, and instead you can use it.
Comments
Post a Comment