2004-04-24 Andreas Nahr <ClassDevelopment@A-SoftTech.com>
[mono.git] / mcs / class / corlib / System / ValueType.cs
1 //
2 // System.ValueType.cs
3 //
4 // Author:
5 //   Miguel de Icaza (miguel@ximian.com)
6 //   Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 //
8 // (C) Ximian, Inc.  http://www.ximian.com
9 // (C) 2003 Novell, Inc.  http://www.novell.com
10 //
11
12 using System.Reflection;
13 using System.Runtime.CompilerServices;
14
15 namespace System
16 {
17         [Serializable]
18         public abstract class ValueType
19         {
20                 protected ValueType ()
21                 {
22                 }
23
24                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
25                 internal extern static bool InternalEquals (object o1, object o2, out object[] fields);
26
27                 // This is also used by RuntimeHelpers
28                 internal static bool DefaultEquals (object o1, object o2)
29                 {
30                         object[] fields;
31
32                         if (o2 == null)
33                                 return false;
34
35                         bool res = InternalEquals (o1, o2, out fields);
36                         if (fields == null)
37                                 return res;
38
39                         for (int i = 0; i < fields.Length; i += 2) {
40                                 object meVal = fields [i];
41                                 object youVal = fields [i + 1];
42                                 if (meVal == null) {
43                                         if (youVal == null)
44                                                 continue;
45
46                                         return false;
47                                 }
48
49                                 if (!meVal.Equals (youVal))
50                                         return false;
51                         }
52
53                         return true;
54                 }
55
56                 // <summary>
57                 //   True if this instance and o represent the same type
58                 //   and have the same value.
59                 // </summary>
60                 public override bool Equals (object o) {
61                         return DefaultEquals (this, o);
62                 }
63
64                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
65                 internal extern static int InternalGetHashCode (object o, out object[] fields);
66
67                 // <summary>
68                 //   Gets a hashcode for this value type using the
69                 //   bits in the structure
70                 // </summary>
71                 public override int GetHashCode ()
72                 {
73                         object[] fields;
74                         int result = InternalGetHashCode (this, out fields);
75
76                         if (fields != null)
77                                 for (int i = 0; i < fields.Length; ++i)
78                                         if (fields [i] != null)
79                                                 result ^= fields [i].GetHashCode ();
80                                 
81                         return result;
82                 }
83
84                 // <summary>
85                 //   Stringified representation of this ValueType.
86                 //   Must be overriden for better results, by default
87                 //   it just returns the Type name.
88                 // </summary>
89                 public override string ToString ()
90                 {
91                         return GetType ().FullName;
92                 }
93         }
94 }