Ik snap dat het wellicht eenvoudiger kan, maar dit is een eerste attribuut die ik nu gemaakt heb, namelijk het 'GreaterThan'-attribuut. Er zal waarschijnlijk ook nog een 'LessThan'-attribuut gemaakt moeten worden. Dus die one-liner functies zijn er vooral voor dat ik die daar makkelijk in kan hergebruiken.
Dat commentaar is een kopie van de tekst van MSDN, maar ik vond het wel even makkelijk om het erbij te houden, omdat ik de CompareTo functie nog niet ken. Zo onthoud ik het beter.

Puur iets wat ik mezelf aangeleerd heb.
Natuurlijk had ik dit
C#:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
| private static bool GreaterThan(object x, object y)
{
return CompareObjectValues(x, y) > 0;
}
private static bool LessThan(object x, object y)
{
return CompareObjectValues(x, y) < 0;
}
private static bool ObjectEquals(object x, object y)
{
return CompareObjectValues(x, y) == 0;
}
/// <summary>
/// Compares the current instance (x) with another object (y) of the same type.
/// </summary>
/// <remarks>
/// ((IComparable)(x)).CompareTo(y)
/// A 32-bit signed integer that indicates the relative order of the comparands. The return value has one of the three meanings described in the following table.
/// </remarks>
/// <param name="x">current instance</param>
/// <param name="y">another object of the same type</param>
/// <returns>
/// Less than 0 (zero) > This object (x) is less than object (y)
/// 0 (zero) > This object (x) is equal to object (y)
/// Greater than 0 (zero) > This object (x) is greater than object (y)
/// </returns>
private static int CompareObjectValues(object x, object y)
{
return ((IComparable) (x)).CompareTo(y);
} |
ook zo kunnen schrijven:
C#:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| private static bool GreaterThan(object x, object y)
{
return ((IComparable) (x)).CompareTo(y) > 0;
}
private static bool LessThan(object x, object y)
{
return ((IComparable) (x)).CompareTo(y) < 0;
}
private static bool ObjectEquals(object x, object y)
{
return ((IComparable) (x)).CompareTo(y) == 0;
} |
maar dan is voor mij niet meteen duidelijk wat
((IComparable) (x)).CompareTo(y) doet. Daarom dat ik dat naar een nieuwe functie heb gezet met daarbij het commentaar wat kleiner dan 0, gelijk aan 0 en groter dan 0 betekend.
[
Voor 62% gewijzigd door
PdeBie op 17-07-2014 15:51
]