Ik heb volgend voorbeeld app gemaakt:
Nu, wanneer je p1.Equals(p2); aanroept dan returns deze false omdat de object types niet overeenstemmen. Dit klopt ergens wel, maar ergens ook niet.
Ik heb het in mijn project voorlopig opgelost met
Maar ik vroeg me af of dit wel de best practice is om met down en upcasting om te gaan?
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
| using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Point p1 = new Point(1, 2); TestPoint tp = new TestPoint(1,2); Point p2 = tp; Test(p1, p2); } static public void Test(Point p1, Point p2) { p1.Equals(p2); } } class Point { public Point(int x, int y) { X = x; Y = y; } public int X { get; set; } public int Y { get; set; } public override bool Equals(object obj) { if (obj == null) return false; if (obj.GetType() != typeof(Point)) return false; Point pt = (Point)obj; if (pt.X != this.X) return false; if (pt.Y != this.Y) return false; return true; } } class TestPoint : Point { public TestPoint(int x, int y) : base(x, y) { } public string name { get; set; } } } |
Nu, wanneer je p1.Equals(p2); aanroept dan returns deze false omdat de object types niet overeenstemmen. Dit klopt ergens wel, maar ergens ook niet.
Ik heb het in mijn project voorlopig opgelost met
C#:
1
| if (obj is Point == false) return false; |
Maar ik vroeg me af of dit wel de best practice is om met down en upcasting om te gaan?
♥ Under Construction ♦ © 1985 - 2013 and counting. ♣ Born to be Root ★ In the end, we are all communists ♠ Please, don't feed me meat