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
12 using nint
= System
.Int64
;
14 using nint
= System
.Int32
;
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)
30 public static readonly IntPtr Zero
;
34 public unsafe IntPtr(int value)
36 _value
= (void*)value;
41 public unsafe IntPtr(long value)
44 _value
= (void*)value;
46 _value
= (void*)checked((int)value);
53 public unsafe IntPtr(void* 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
);
68 unsafe void ISerializable
.GetObjectData(SerializationInfo info
, StreamingContext context
)
71 throw new ArgumentNullException(nameof(info
));
73 info
.AddValue("value", ToInt64());
76 public override unsafe bool Equals(object? obj
)
80 return (_value
== ((IntPtr
)obj
)._value
);
85 unsafe bool IEquatable
<IntPtr
>.Equals(IntPtr other
)
87 return _value
== other
._value
;
90 public override unsafe int GetHashCode()
93 long l
= (long)_value
;
94 return (unchecked((int)l
) ^
(int)(l
>> 32));
96 return unchecked((int)_value
);
102 public unsafe int ToInt32()
105 long l
= (long)_value
;
106 return checked((int)l
);
114 public unsafe long ToInt64()
121 public static unsafe explicit operator IntPtr(int value)
123 return new IntPtr(value);
128 public static unsafe explicit operator IntPtr(long value)
130 return new IntPtr(value);
133 [CLSCompliant(false)]
136 public static unsafe explicit operator IntPtr(void* value)
138 return new IntPtr(value);
141 [CLSCompliant(false)]
144 public static unsafe explicit operator void* (IntPtr
value)
151 public static unsafe explicit operator int(IntPtr
value)
154 long l
= (long)value._value
;
155 return checked((int)l
);
157 return (int)value._value
;
163 public static unsafe explicit operator long(IntPtr
value)
165 return (nint
)value._value
;
170 public static unsafe bool operator ==(IntPtr value1
, IntPtr value2
)
172 return value1
._value
== value2
._value
;
177 public static unsafe bool operator !=(IntPtr value1
, IntPtr value2
)
179 return value1
._value
!= value2
._value
;
183 public static IntPtr
Add(IntPtr pointer
, int offset
)
185 return pointer
+ offset
;
190 public static unsafe IntPtr
operator +(IntPtr pointer
, int offset
)
192 return new IntPtr((nint
)pointer
._value
+ offset
);
196 public static IntPtr
Subtract(IntPtr pointer
, int offset
)
198 return pointer
- offset
;
203 public static unsafe IntPtr
operator -(IntPtr pointer
, int offset
)
205 return new IntPtr((nint
)pointer
._value
- offset
);
208 public static int Size
218 [CLSCompliant(false)]
221 public unsafe void* ToPointer()
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
);