Fix StyleCop warning SA1206 (modifer ordering)
[mono-project.git] / netcore / System.Private.CoreLib / shared / System / IntPtr.cs
blob4a5ca1fb2715a736c965c69a25242dbad7991cf3
1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
5 using System.Globalization;
6 using System.Runtime.CompilerServices;
7 using System.Runtime.Serialization;
8 using System.Runtime.Versioning;
10 #pragma warning disable SA1121 // explicitly using type aliases instead of built-in types
11 #if BIT64
12 using nint = System.Int64;
13 #else
14 using nint = System.Int32;
15 #endif
17 namespace System
19 [Serializable]
20 [System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
21 public readonly struct IntPtr : IEquatable<IntPtr>, ISerializable
23 // WARNING: We allow diagnostic tools to directly inspect this member (_value).
24 // See https://github.com/dotnet/corert/blob/master/Documentation/design-docs/diagnostics/diagnostics-tools-contract.md for more details.
25 // Please do not change the type, the name, or the semantic usage of this member without understanding the implication for tools.
26 // Get in touch with the diagnostics team if you have questions.
27 private readonly unsafe void* _value; // Do not rename (binary serialization)
29 [Intrinsic]
30 public static readonly IntPtr Zero;
32 [Intrinsic]
33 [NonVersionable]
34 public unsafe IntPtr(int value)
36 _value = (void*)value;
39 [Intrinsic]
40 [NonVersionable]
41 public unsafe IntPtr(long value)
43 #if BIT64
44 _value = (void*)value;
45 #else
46 _value = (void*)checked((int)value);
47 #endif
50 [CLSCompliant(false)]
51 [Intrinsic]
52 [NonVersionable]
53 public unsafe IntPtr(void* value)
55 _value = value;
58 private unsafe IntPtr(SerializationInfo info, StreamingContext context)
60 long l = info.GetInt64("value");
62 if (Size == 4 && (l > int.MaxValue || l < int.MinValue))
63 throw new ArgumentException(SR.Serialization_InvalidPtrValue);
65 _value = (void*)l;
68 unsafe void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
70 if (info == null)
71 throw new ArgumentNullException(nameof(info));
73 info.AddValue("value", ToInt64());
76 public override unsafe bool Equals(object? obj)
78 if (obj is IntPtr)
80 return (_value == ((IntPtr)obj)._value);
82 return false;
85 unsafe bool IEquatable<IntPtr>.Equals(IntPtr other)
87 return _value == other._value;
90 public override unsafe int GetHashCode()
92 #if BIT64
93 long l = (long)_value;
94 return (unchecked((int)l) ^ (int)(l >> 32));
95 #else
96 return unchecked((int)_value);
97 #endif
100 [Intrinsic]
101 [NonVersionable]
102 public unsafe int ToInt32()
104 #if BIT64
105 long l = (long)_value;
106 return checked((int)l);
107 #else
108 return (int)_value;
109 #endif
112 [Intrinsic]
113 [NonVersionable]
114 public unsafe long ToInt64()
116 return (nint)_value;
119 [Intrinsic]
120 [NonVersionable]
121 public static unsafe explicit operator IntPtr(int value)
123 return new IntPtr(value);
126 [Intrinsic]
127 [NonVersionable]
128 public static unsafe explicit operator IntPtr(long value)
130 return new IntPtr(value);
133 [CLSCompliant(false)]
134 [Intrinsic]
135 [NonVersionable]
136 public static unsafe explicit operator IntPtr(void* value)
138 return new IntPtr(value);
141 [CLSCompliant(false)]
142 [Intrinsic]
143 [NonVersionable]
144 public static unsafe explicit operator void* (IntPtr value)
146 return value._value;
149 [Intrinsic]
150 [NonVersionable]
151 public static unsafe explicit operator int(IntPtr value)
153 #if BIT64
154 long l = (long)value._value;
155 return checked((int)l);
156 #else
157 return (int)value._value;
158 #endif
161 [Intrinsic]
162 [NonVersionable]
163 public static unsafe explicit operator long(IntPtr value)
165 return (nint)value._value;
168 [Intrinsic]
169 [NonVersionable]
170 public static unsafe bool operator ==(IntPtr value1, IntPtr value2)
172 return value1._value == value2._value;
175 [Intrinsic]
176 [NonVersionable]
177 public static unsafe bool operator !=(IntPtr value1, IntPtr value2)
179 return value1._value != value2._value;
182 [NonVersionable]
183 public static IntPtr Add(IntPtr pointer, int offset)
185 return pointer + offset;
188 [Intrinsic]
189 [NonVersionable]
190 public static unsafe IntPtr operator +(IntPtr pointer, int offset)
192 return new IntPtr((nint)pointer._value + offset);
195 [NonVersionable]
196 public static IntPtr Subtract(IntPtr pointer, int offset)
198 return pointer - offset;
201 [Intrinsic]
202 [NonVersionable]
203 public static unsafe IntPtr operator -(IntPtr pointer, int offset)
205 return new IntPtr((nint)pointer._value - offset);
208 public static int Size
210 [Intrinsic]
211 [NonVersionable]
214 return sizeof(nint);
218 [CLSCompliant(false)]
219 [Intrinsic]
220 [NonVersionable]
221 public unsafe void* ToPointer()
223 return _value;
226 public override unsafe string ToString()
228 return ((nint)_value).ToString(CultureInfo.InvariantCulture);
231 public unsafe string ToString(string format)
233 return ((nint)_value).ToString(format, CultureInfo.InvariantCulture);