.


:




:

































 

 

 

 


. Equals (), .




bool Equals( other)

, other. true, , false.

IEquatable<T> GetHashCode () Equals (Object), Object, Equals (). , Is In ().

// IEquatable<T>.

public static bool IsIn<T>(T what, T[] obs) where T: IEquatable<T> { foreach(T v in obs)

if(v.Equals(what)) // Equals(). return true;

Return false;

}

.

where : IEquatable<T>

, , IEquatable, Is In (). Equals (), .

I Comp able. : . , . IComparable<T> .

public interface IComparable<T>

. CompareTo (), .

int CompareTo( other)

, other. , , other; , , other.

CompareTo (), , IComparable<T>. CompareTo (), .

IComparable<T>. InRange (), true, .

// IComparable<T>. // , . // true, what // , obs.

public static bool InRange<T>(T what, T[] obs) where T: IComparable<T> { if(what.CompareTo(obs[0]) < 0 ||

what.CompareTo(obs[obs.Length-1]) > 0) return false; return true;

}

Is In () InRange () .

// // IComparable<T> IEquatable<T>.

Using System;

// MyClass // IComparable<T> IEquatable<T>. class MyClass: IComparable<MyClass>, IEquatable<MyClass> { public int Val;

public MyClass(int x) { Val = x; }

// IComparable<T>. public int CompareTo(MyClass other) {

return Val - other.Val; // Now, no cast is needed.

> \

// IEquatable<T>.

public bool Equals(MyClass other) { return Val == other.Val;

}

// Equals(Object).

public override bool Equals(Object obj) { if(obj is MyClass)





:


: 2016-12-03; !; : 359 |


:

:

- , , .
==> ...

1759 - | 1545 -


© 2015-2024 lektsii.org - -

: 0.011 .