-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterfaceOverloads.cs
38 lines (31 loc) · 1.24 KB
/
interfaceOverloads.cs
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
34
35
36
37
38
using System;
// This class implements the generic IComparable<T> interface twice
public sealed class Number: IComparable<Int32>, IComparable<String> {
private Int32 m_val = 5;
// You are allowed to implement both of these methods
// because they have different signatures and are valid
// interface implementations.
// This method implements IComparable<Int32>'s CompareTo
public Int32 CompareTo(Int32 n) {
return m_val.CompareTo(n);
}
// This method implements IComparable<String>'s CompareTo
public Int32 CompareTo(String s) {
//here we automatically rollup the conversion from String to Int32.
//Reducing the amount of casts required to use the class.
return m_val.CompareTo(Int32.Parse(s));
}
}
public static class Program {
public static void Main() {
Number n = new Number();
//m_val = 5 here due to default field.
// Here, I compare the value in n with an Int32 (5)
IComparable<Int32> cInt32 = n;
Int32 result = cInt32.CompareTo(5);
// Here, I compare the value in n with a String ("5")
IComparable<String> cString = n;
result = cString.CompareTo("5");
}
}
//Richter, Jeffrey. CLR via C# (Developer Reference) . Pearson Education. Kindle Edition.