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 nuint
= System
.UInt64
;
14 using nuint
= System
.UInt32
;
21 [System
.Runtime
.CompilerServices
.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
22 public readonly struct UIntPtr
: IEquatable
<UIntPtr
>, ISerializable
24 private readonly unsafe void* _value
; // Do not rename (binary serialization)
27 public static readonly UIntPtr Zero
;
31 public unsafe UIntPtr(uint value)
33 _value
= (void*)value;
38 public unsafe UIntPtr(ulong value)
41 _value
= (void*)value;
43 _value
= (void*)checked((uint)value);
49 public unsafe UIntPtr(void* value)
54 private unsafe UIntPtr(SerializationInfo info
, StreamingContext context
)
56 ulong l
= info
.GetUInt64("value");
58 if (Size
== 4 && l
> uint.MaxValue
)
59 throw new ArgumentException(SR
.Serialization_InvalidPtrValue
);
64 void ISerializable
.GetObjectData(SerializationInfo info
, StreamingContext context
)
67 throw new ArgumentNullException(nameof(info
));
69 info
.AddValue("value", ToUInt64());
72 public unsafe override bool Equals(object? obj
)
76 return (_value
== ((UIntPtr
)obj
)._value
);
81 unsafe bool IEquatable
<UIntPtr
>.Equals(UIntPtr other
)
83 return _value
== other
._value
;
86 public unsafe override int GetHashCode()
89 ulong l
= (ulong)_value
;
90 return (unchecked((int)l
) ^
(int)(l
>> 32));
92 return unchecked((int)_value
);
98 public unsafe uint ToUInt32()
101 return checked((uint)_value
);
109 public unsafe ulong ToUInt64()
111 return (ulong)_value
;
116 public static explicit operator UIntPtr(uint value)
118 return new UIntPtr(value);
123 public static explicit operator UIntPtr(ulong value)
125 return new UIntPtr(value);
130 public static unsafe explicit operator UIntPtr(void* value)
132 return new UIntPtr(value);
137 public static unsafe explicit operator void* (UIntPtr
value)
144 public static unsafe explicit operator uint(UIntPtr
value)
147 return checked((uint)value._value
);
149 return (uint)value._value
;
155 public static unsafe explicit operator ulong(UIntPtr
value)
157 return (ulong)value._value
;
162 public static unsafe bool operator ==(UIntPtr value1
, UIntPtr value2
)
164 return value1
._value
== value2
._value
;
169 public static unsafe bool operator !=(UIntPtr value1
, UIntPtr value2
)
171 return value1
._value
!= value2
._value
;
175 public static UIntPtr
Add(UIntPtr pointer
, int offset
)
177 return pointer
+ offset
;
182 public static unsafe UIntPtr
operator +(UIntPtr pointer
, int offset
)
184 return new UIntPtr((nuint
)pointer
._value
+ (nuint
)offset
);
188 public static UIntPtr
Subtract(UIntPtr pointer
, int offset
)
190 return pointer
- offset
;
195 public static unsafe UIntPtr
operator -(UIntPtr pointer
, int offset
)
197 return new UIntPtr((nuint
)pointer
._value
- (nuint
)offset
);
200 public static int Size
206 return sizeof(nuint
);
212 public unsafe void* ToPointer()
217 public unsafe override string ToString()
219 return ((nuint
)_value
).ToString(CultureInfo
.InvariantCulture
);