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
;
11 using nuint
= System
.UInt64
;
13 using nuint
= System
.UInt32
;
20 [System
.Runtime
.CompilerServices
.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
21 public readonly struct UIntPtr
: IEquatable
<UIntPtr
>, ISerializable
23 private readonly unsafe void* _value
; // Do not rename (binary serialization)
26 public static readonly UIntPtr Zero
;
30 public unsafe UIntPtr(uint value)
32 _value
= (void*)value;
37 public unsafe UIntPtr(ulong value)
40 _value
= (void*)value;
42 _value
= (void*)checked((uint)value);
48 public unsafe UIntPtr(void* value)
53 private unsafe UIntPtr(SerializationInfo info
, StreamingContext context
)
55 ulong l
= info
.GetUInt64("value");
57 if (Size
== 4 && l
> uint.MaxValue
)
58 throw new ArgumentException(SR
.Serialization_InvalidPtrValue
);
63 void ISerializable
.GetObjectData(SerializationInfo info
, StreamingContext context
)
66 throw new ArgumentNullException(nameof(info
));
68 info
.AddValue("value", ToUInt64());
71 public unsafe override bool Equals(object? obj
)
75 return (_value
== ((UIntPtr
)obj
)._value
);
80 unsafe bool IEquatable
<UIntPtr
>.Equals(UIntPtr other
)
82 return _value
== other
._value
;
85 public unsafe override int GetHashCode()
88 ulong l
= (ulong)_value
;
89 return (unchecked((int)l
) ^
(int)(l
>> 32));
91 return unchecked((int)_value
);
97 public unsafe uint ToUInt32()
100 return checked((uint)_value
);
108 public unsafe ulong ToUInt64()
110 return (ulong)_value
;
115 public static explicit operator UIntPtr(uint value)
117 return new UIntPtr(value);
122 public static explicit operator UIntPtr(ulong value)
124 return new UIntPtr(value);
129 public static unsafe explicit operator UIntPtr(void* value)
131 return new UIntPtr(value);
136 public static unsafe explicit operator void* (UIntPtr
value)
143 public static unsafe explicit operator uint(UIntPtr
value)
146 return checked((uint)value._value
);
148 return (uint)value._value
;
154 public static unsafe explicit operator ulong(UIntPtr
value)
156 return (ulong)value._value
;
161 public static unsafe bool operator ==(UIntPtr value1
, UIntPtr value2
)
163 return value1
._value
== value2
._value
;
168 public static unsafe bool operator !=(UIntPtr value1
, UIntPtr value2
)
170 return value1
._value
!= value2
._value
;
174 public static UIntPtr
Add(UIntPtr pointer
, int offset
)
176 return pointer
+ offset
;
181 public static unsafe UIntPtr
operator +(UIntPtr pointer
, int offset
)
183 return new UIntPtr((nuint
)pointer
._value
+ (nuint
)offset
);
187 public static UIntPtr
Subtract(UIntPtr pointer
, int offset
)
189 return pointer
- offset
;
194 public static unsafe UIntPtr
operator -(UIntPtr pointer
, int offset
)
196 return new UIntPtr((nuint
)pointer
._value
- (nuint
)offset
);
199 public static int Size
205 return sizeof(nuint
);
211 public unsafe void* ToPointer()
216 public unsafe override string ToString()
218 return ((nuint
)_value
).ToString(CultureInfo
.InvariantCulture
);