• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

realm / realm-dotnet / 9681982895

26 Jun 2024 02:52PM UTC coverage: 81.304% (-0.05%) from 81.358%
9681982895

Pull #3635

github

6c02a1
nirinchev
Add some scaffolding for the flexible schema poc
Pull Request #3635: Add flexible schema mapping POC

2312 of 2997 branches covered (77.14%)

Branch coverage included in aggregate %.

5 of 10 new or added lines in 1 file covered. (50.0%)

7 existing lines in 1 file now uncovered.

6838 of 8257 relevant lines covered (82.81%)

42533.79 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.92
/Realm/Realm/DatabaseTypes/RealmValue.cs
1
////////////////////////////////////////////////////////////////////////////
2
//
3
// Copyright 2020 Realm Inc.
4
//
5
// Licensed under the Apache License, Version 2.0 (the "License");
6
// you may not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
8
//
9
// http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing, software
12
// distributed under the License is distributed on an "AS IS" BASIS,
13
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
// See the License for the specific language governing permissions and
15
// limitations under the License.
16
//
17
////////////////////////////////////////////////////////////////////////////
18

19
using System;
20
using System.Buffers;
21
using System.Collections.Generic;
22
using System.ComponentModel;
23
using System.Diagnostics;
24
using System.Linq;
25
using System.Runtime.InteropServices;
26
using System.Text;
27
using MongoDB.Bson;
28
using Realms.Extensions;
29
using Realms.Helpers;
30
using Realms.Native;
31

32
namespace Realms
33
{
34
    /// <summary>
35
    /// A type that can represent any valid Realm data type. It is a valid type in and of itself, which
36
    /// means that it can be used to declare a property of type <see cref="RealmValue"/>.
37
    /// Please note that a <see cref="RealmValue"/> property in a managed <see cref="IRealmObjectBase">realm object</see>
38
    /// cannot contain an <see cref="IEmbeddedObject">embedded object</see> or an <see cref="IAsymmetricObject">asymmetric object</see>.
39
    /// </summary>
40
    /// <example>
41
    /// <code>
42
    /// public class MyClass : RealmObject
43
    /// {
44
    ///     public RealmValue MyValue { get; set; }
45
    /// }
46
    ///
47
    /// var obj = new MyClass();
48
    /// obj.MyValue = 123;
49
    /// obj.MyValue = "abc";
50
    ///
51
    /// if (obj.Type == RealmValueType.Int)
52
    /// {
53
    ///     var myInt = obj.MyValue.AsLong();
54
    /// }
55
    /// </code>
56
    /// </example>
57
    [Preserve(AllMembers = true)]
58
    [DebuggerDisplay("Type = {Type}, Value = {ToString(),nq}")]
59
    [StructLayout(LayoutKind.Explicit)]
60
    public readonly struct RealmValue : IEquatable<RealmValue>
61
    {
62
        [FieldOffset(0)]
63
        private readonly PrimitiveValue _primitiveValue;
64

65
        // _intValue has been extracted here to be more memory efficient, as it needs also _propertyIndex
66
        // and _objectHandle when working with RealmInteger.
67
        [FieldOffset(0)]
68
        private readonly long _intValue;
69

70
        // This is only used when PrimitiveValue.Type == Int.
71
        [FieldOffset(8)]
72
        private readonly IntPtr _propertyIndex;
73

74
        // This occupies the same memory space as PrimitiveValue.Type.
75
        [FieldOffset(16)]
76
        private readonly RealmValueType _type;
77

78
        // Object fields cannot be at the same offset as non-object fields,
79
        // thus all the following values cannot overlap _primitiveValue
80
        // even though some are mutually exclusive.
81
        [FieldOffset(24)]
82
        private readonly string? _stringValue;
83

84
        [FieldOffset(24)]
85
        private readonly byte[]? _dataValue;
86

87
        [FieldOffset(24)]
88
        private readonly IRealmObjectBase? _objectValue;
89

90
        [FieldOffset(24)]
91
        private readonly IList<RealmValue>? _listValue;
92

93
        [FieldOffset(24)]
94
        private readonly IDictionary<string, RealmValue>? _dictionaryValue;
95

96
        // This is only used when PrimitiveValue.Type == Int.
97
        [FieldOffset(24)]
98
        private readonly ObjectHandle? _objectHandle;
99

100
        /// <summary>
101
        /// Gets the <see cref="RealmValueType"/> stored in this value.
102
        /// </summary>
103
        /// <remarks>
104
        /// You can check the type of the Realm value and then use any of the AsXXX methods to convert it to correct C# type.
105
        /// <br/>
106
        /// For performance reasons, all integral types, i.e. <see cref="byte"/>, <see cref="short"/>, <see cref="int"/>, <see cref="long"/>,
107
        /// as well as <see cref="char"/> are represented as <see cref="RealmValueType.Int"/>. Realm preserves no information about the original
108
        /// type of the integral value stored in a <see cref="RealmValue"/> field.
109
        /// </remarks>
110
        /// <value>The <see cref="RealmValueType"/> of the current value in the database.</value>
111
        public RealmValueType Type => _type;
575,359✔
112

113
        internal RealmValue(PrimitiveValue primitive, Realm? realm = null, ObjectHandle? handle = default, IntPtr propertyIndex = default) : this()
113,312✔
114
        {
115
            _type = primitive.Type;
113,312✔
116

117
            switch (Type)
113,312✔
118
            {
119
                case RealmValueType.Data:
120
                    _dataValue = primitive.AsBinary();
2,584✔
121
                    break;
2,584✔
122
                case RealmValueType.String:
123
                    _stringValue = primitive.AsString();
5,205✔
124
                    break;
5,205✔
125
                case RealmValueType.Object:
126
                    Argument.NotNull(realm, nameof(realm));
6,258✔
127
                    _objectValue = primitive.AsObject(realm!);
6,258✔
128
                    break;
6,258✔
129
                case RealmValueType.List:
130
                    Argument.NotNull(realm, nameof(realm));
286✔
131
                    _listValue = primitive.AsList(realm!);
286✔
132
                    break;
286✔
133
                case RealmValueType.Dictionary:
134
                    Argument.NotNull(realm, nameof(realm));
219✔
135
                    _dictionaryValue = primitive.AsDictionary(realm!);
219✔
136
                    break;
219✔
137
                case RealmValueType.Int:
138
                    _intValue = primitive.AsInt();
14,325✔
139
                    _propertyIndex = propertyIndex;
14,325✔
140
                    _objectHandle = handle;
14,325✔
141
                    break;
14,325✔
142
                default:
143
                    _primitiveValue = primitive;
84,435✔
144
                    break;
145
            }
146
        }
84,435✔
147

148
        private RealmValue(long intValue) : this()
23,331✔
149
        {
150
            _type = RealmValueType.Int;
23,331✔
151
            _intValue = intValue;
23,331✔
152
        }
23,331✔
153

154
        private RealmValue(byte[] data) : this()
4,475✔
155
        {
156
            _type = RealmValueType.Data;
4,475✔
157
            _dataValue = data;
4,475✔
158
        }
4,475✔
159

160
        private RealmValue(string value) : this()
26,163✔
161
        {
162
            _type = RealmValueType.String;
26,163✔
163
            _stringValue = value;
26,163✔
164
        }
26,163✔
165

166
        private RealmValue(IRealmObjectBase obj) : this()
4,055✔
167
        {
168
            _type = RealmValueType.Object;
4,055✔
169
            _objectValue = obj;
4,055✔
170
        }
4,055✔
171

172
        private RealmValue(IList<RealmValue> list) : this()
98✔
173
        {
174
            _type = RealmValueType.List;
98✔
175
            _listValue = list;
98✔
176
        }
98✔
177

178
        private RealmValue(IDictionary<string, RealmValue> dict) : this()
91✔
179
        {
180
            _type = RealmValueType.Dictionary;
91✔
181
            _dictionaryValue = dict;
91✔
182
        }
91✔
183

184
        /// <summary>
185
        /// Gets a RealmValue representing <c>null</c>.
186
        /// </summary>
187
        /// <value>A new RealmValue instance of type <see cref="Null"/>.</value>
188
        public static RealmValue Null => new(PrimitiveValue.Null());
12,020✔
189

190
        private static RealmValue Bool(bool value) => new(PrimitiveValue.Bool(value));
3,947✔
191

192
        private static RealmValue Float(float value) => new(PrimitiveValue.Float(value));
4,823✔
193

194
        private static RealmValue Double(double value) => new(PrimitiveValue.Double(value));
6,053✔
195

196
        private static RealmValue Date(DateTimeOffset value) => new(PrimitiveValue.Date(value));
5,668✔
197

198
        private static RealmValue Decimal(Decimal128 value) => new(PrimitiveValue.Decimal(value));
9,978✔
199

200
        private static RealmValue ObjectId(ObjectId value) => new(PrimitiveValue.ObjectId(value));
10,890✔
201

202
        private static RealmValue Guid(Guid value) => new(PrimitiveValue.Guid(value));
915✔
203

204
        private static RealmValue Int(long value) => new(value);
23,331✔
205

206
        private static RealmValue Data(byte[] value) => new(value);
4,475✔
207

208
        private static RealmValue String(string value) => new(value);
26,163✔
209

210
        [EditorBrowsable(EditorBrowsableState.Never)]
211
        public static RealmValue Object(IRealmObjectBase value) => new(value);
4,055✔
212

213
        /// <summary>
214
        /// Gets a RealmValue representing a list.
215
        /// </summary>
216
        /// <param name="value"> The input list to copy. </param>
217
        /// <returns> A new RealmValue representing the input list. </returns>
218
        /// <remarks> Once created, this RealmValue will just wrap the input collection.
219
        /// After the object containing this RealmValue gets managed this value will be a Realm list.</remarks>
220
        public static RealmValue List(IList<RealmValue> value) => new(value);
98✔
221

222
        /// <summary>
223
        /// Gets a RealmValue representing a dictionary.
224
        /// </summary>
225
        /// <param name="value"> The input dictionary to copy. </param>
226
        /// <returns> A new RealmValue representing the input dictionary. </returns>
227
        /// <remarks> Once created, this RealmValue will just wrap the input collection.
228
        /// After the object containing this RealmValue gets managed this value will be a Realm dictionary.</remarks>
229
        public static RealmValue Dictionary(IDictionary<string, RealmValue> value) => new(value);
91✔
230

231
        internal static RealmValue Create<T>(T value, RealmValueType type)
232
        {
233
            if (value is null)
225✔
234
            {
235
                return new RealmValue(PrimitiveValue.Null());
18✔
236
            }
237

238
            return type switch
207!
239
            {
207✔
240
                RealmValueType.String => String(Operator.Convert<T, string>(value)),
50✔
241
                RealmValueType.Data => Data(Operator.Convert<T, byte[]>(value)),
12✔
242
                RealmValueType.Bool => Bool(Operator.Convert<T, bool>(value)),
12✔
243
                RealmValueType.Int => Int(Operator.Convert<T, long>(value)),
27✔
244
                RealmValueType.Float => Float(Operator.Convert<T, float>(value)),
12✔
245
                RealmValueType.Double => Double(Operator.Convert<T, double>(value)),
12✔
246
                RealmValueType.Date => Date(Operator.Convert<T, DateTimeOffset>(value)),
11✔
247
                RealmValueType.Decimal128 => Decimal(Operator.Convert<T, Decimal128>(value)),
32✔
248
                RealmValueType.ObjectId => ObjectId(Operator.Convert<T, ObjectId>(value)),
11✔
249
                RealmValueType.Guid => Guid(Operator.Convert<T, Guid>(value)),
13✔
250
                RealmValueType.Object => Object(Operator.Convert<T, IRealmObjectBase>(value)),
11✔
251
                RealmValueType.List => List(Operator.Convert<T, IList<RealmValue>>(value)),
2✔
252
                RealmValueType.Dictionary => Dictionary(Operator.Convert<T, IDictionary<string, RealmValue>>(value)),
2✔
253
                _ => throw new NotSupportedException($"RealmValueType {type} is not supported."),
×
254
            };
207✔
255
        }
256

257
        internal (PrimitiveValue Value, HandlesToCleanup? Handles) ToNative()
258
        {
259
            switch (Type)
104,535✔
260
            {
261
                case RealmValueType.String:
262
                    if (_stringValue == null)
25,699!
263
                    {
264
                        return (PrimitiveValue.Null(), null);
×
265
                    }
266

267
                    var buffer = ArrayPool<byte>.Shared.Rent(Encoding.UTF8.GetMaxByteCount(_stringValue.Length));
25,699✔
268
                    var bytes = Encoding.UTF8.GetBytes(_stringValue, 0, _stringValue.Length, buffer, 0);
25,699✔
269
                    var stringHandle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
25,699✔
270
                    return (PrimitiveValue.String(stringHandle.AddrOfPinnedObject(), bytes), new HandlesToCleanup(stringHandle, buffer));
25,699✔
271
                case RealmValueType.Data:
272
                    if (_dataValue == null)
4,595!
273
                    {
274
                        return (PrimitiveValue.Null(), null);
×
275
                    }
276

277
                    var handle = GCHandle.Alloc(_dataValue, GCHandleType.Pinned);
4,595✔
278
                    return (PrimitiveValue.Data(handle.AddrOfPinnedObject(), _dataValue?.Length ?? 0), new HandlesToCleanup(handle));
4,595!
279
                case RealmValueType.Object:
280
                    var obj = AsIRealmObject();
3,772✔
281
                    if (!obj.IsManaged)
3,772!
282
                    {
283
                        throw new InvalidOperationException("Can't convert unmanaged object to native");
×
284
                    }
285

286
                    return (PrimitiveValue.Object(obj.GetObjectHandle()!), null);
3,772✔
287
                default:
288
                    return (_primitiveValue, null);
70,469✔
289
            }
290
        }
291

292
        /// <summary>
293
        /// Returns the stored value as a <see cref="char"/>.
294
        /// </summary>
295
        /// <exception cref="InvalidOperationException">Thrown if the underlying value is not of type <see cref="RealmValueType.Int"/>.</exception>
296
        /// <returns>A UTF-16 code unit representing the value stored in the database.</returns>
297
        public char AsChar()
298
        {
299
            EnsureType("char", RealmValueType.Int);
170✔
300
            return (char)AsInt64();
170✔
301
        }
302

303
        /// <summary>
304
        /// Returns the stored value as a <see cref="byte"/>.
305
        /// </summary>
306
        /// <exception cref="InvalidOperationException">Thrown if the underlying value is not of type <see cref="RealmValueType.Int"/>.</exception>
307
        /// <returns>An 8-bit unsigned integer representing the value stored in the database.</returns>
308
        /// <seealso cref="AsByteRealmInteger"/>
309
        public byte AsByte()
310
        {
311
            EnsureType("byte", RealmValueType.Int);
549✔
312
            return (byte)AsInt64();
549✔
313
        }
314

315
        /// <summary>
316
        /// Returns the stored value as a <see cref="short"/> (Int16).
317
        /// </summary>
318
        /// <exception cref="InvalidOperationException">Thrown if the underlying value is not of type <see cref="RealmValueType.Int"/>.</exception>
319
        /// <returns>A 16-bit integer representing the value stored in the database.</returns>
320
        /// <seealso cref="AsInt16RealmInteger"/>
321
        public short AsInt16()
322
        {
323
            EnsureType("short", RealmValueType.Int);
579✔
324
            return (short)AsInt64();
577✔
325
        }
326

327
        /// <summary>
328
        /// Returns the stored value as an <see cref="int"/> (Int32).
329
        /// </summary>
330
        /// <exception cref="InvalidOperationException">Thrown if the underlying value is not of type <see cref="RealmValueType.Int"/>.</exception>
331
        /// <returns>A 32-bit integer representing the value stored in the database.</returns>
332
        /// <seealso cref="AsInt32RealmInteger"/>
333
        public int AsInt32()
334
        {
335
            EnsureType("int", RealmValueType.Int);
1,703✔
336
            return (int)AsInt64();
1,703✔
337
        }
338

339
        /// <summary>
340
        /// Returns the stored value as a <see cref="long"/> (Int64).
341
        /// </summary>
342
        /// <exception cref="InvalidOperationException">Thrown if the underlying value is not of type <see cref="RealmValueType.Int"/>.</exception>
343
        /// <returns>A 64-bit integer representing the value stored in the database.</returns>
344
        /// <seealso cref="AsInt64RealmInteger"/>
345
        public long AsInt64()
346
        {
347
            EnsureType("long", RealmValueType.Int);
19,241✔
348
            return _intValue;
19,241✔
349
        }
350

351
        /// <summary>
352
        /// Returns the stored value as a <see cref="float"/>.
353
        /// </summary>
354
        /// <exception cref="InvalidOperationException">Thrown if the underlying value is not of type <see cref="RealmValueType.Float"/>.</exception>
355
        /// <returns>A 32-bit floating point number representing the value stored in the database.</returns>
356
        public float AsFloat()
357
        {
358
            EnsureType("float", RealmValueType.Float);
3,432✔
359
            return _primitiveValue.AsFloat();
3,431✔
360
        }
361

362
        /// <summary>
363
        /// Returns the stored value as a <see cref="double"/>.
364
        /// </summary>
365
        /// <exception cref="InvalidOperationException">Thrown if the underlying value is not of type <see cref="RealmValueType.Double"/>.</exception>
366
        /// <returns>A 64-bit floating point number representing the value stored in the database.</returns>
367
        public double AsDouble()
368
        {
369
            EnsureType("double", RealmValueType.Double);
3,881✔
370
            return _primitiveValue.AsDouble();
3,881✔
371
        }
372

373
        /// <summary>
374
        /// Returns the stored value as a <see cref="bool"/>.
375
        /// </summary>
376
        /// <exception cref="InvalidOperationException">Thrown if the underlying value is not of type <see cref="RealmValueType.Bool"/>.</exception>
377
        /// <returns>A boolean representing the value stored in the database.</returns>
378
        public bool AsBool()
379
        {
380
            EnsureType("bool", RealmValueType.Bool);
2,742✔
381
            return _primitiveValue.AsBool();
2,742✔
382
        }
383

384
        /// <summary>
385
        /// Returns the stored value as a <see cref="DateTimeOffset"/>.
386
        /// </summary>
387
        /// <exception cref="InvalidOperationException">Thrown if the underlying value is not of type <see cref="RealmValueType.Date"/>.</exception>
388
        /// <returns>A DateTimeOffset value representing the value stored in the database.</returns>
389
        public DateTimeOffset AsDate()
390
        {
391
            EnsureType("date", RealmValueType.Date);
3,398✔
392
            return _primitiveValue.AsDate();
3,398✔
393
        }
394

395
        /// <summary>
396
        /// Returns the stored value as a <see cref="decimal"/>.
397
        /// </summary>
398
        /// <exception cref="InvalidOperationException">Thrown if the underlying value is not of type <see cref="RealmValueType.Decimal128"/>.</exception>
399
        /// <returns>A 96-bit decimal number representing the value stored in the database.</returns>
400
        public decimal AsDecimal()
401
        {
402
            EnsureType("decimal", RealmValueType.Decimal128);
212✔
403
            return (decimal)_primitiveValue.AsDecimal();
212✔
404
        }
405

406
        /// <summary>
407
        /// Returns the stored value as a <see cref="Decimal128"/>.
408
        /// </summary>
409
        /// <exception cref="InvalidOperationException">Thrown if the underlying value is not of type <see cref="RealmValueType.Decimal128"/>.</exception>
410
        /// <returns>A 128-bit decimal number representing the value stored in the database.</returns>
411
        public Decimal128 AsDecimal128()
412
        {
413
            EnsureType("Decimal128", RealmValueType.Decimal128);
8,431✔
414
            return _primitiveValue.AsDecimal();
8,431✔
415
        }
416

417
        /// <summary>
418
        /// Returns the stored value as an <see cref="MongoDB.Bson.ObjectId"/>.
419
        /// </summary>
420
        /// <exception cref="InvalidOperationException">Thrown if the underlying value is not of type <see cref="RealmValueType.ObjectId"/>.</exception>
421
        /// <returns>An ObjectId representing the value stored in the database.</returns>
422
        public ObjectId AsObjectId()
423
        {
424
            EnsureType("ObjectId", RealmValueType.ObjectId);
10,000✔
425
            return _primitiveValue.AsObjectId();
10,000✔
426
        }
427

428
        /// <summary>
429
        /// Returns the stored value as a <see cref="System.Guid"/>.
430
        /// </summary>
431
        /// <exception cref="InvalidOperationException">Thrown if the underlying value is not of type <see cref="RealmValueType.Guid"/>.</exception>
432
        /// <returns>A Guid representing the value stored in the database.</returns>
433
        public Guid AsGuid()
434
        {
435
            EnsureType("Guid", RealmValueType.Guid);
1,430✔
436
            return _primitiveValue.AsGuid();
1,429✔
437
        }
438

439
        /// <summary>
440
        /// Returns the stored value as a <see cref="RealmInteger{T}"/>. It offers Increment/Decrement API that preserve intent when merging
441
        /// conflicts.
442
        /// </summary>
443
        /// <exception cref="InvalidOperationException">Thrown if the underlying value is not of type <see cref="RealmValueType.Int"/>.</exception>
444
        /// <returns>An 8-bit <see cref="RealmInteger{T}"/> representing the value stored in the database.</returns>
445
        /// <seealso cref="AsByte"/>
446
        public RealmInteger<byte> AsByteRealmInteger() => AsRealmInteger(AsByte());
210✔
447

448
        /// <summary>
449
        /// Returns the stored value as a <see cref="RealmInteger{T}"/>. It offers Increment/Decrement API that preserve intent when merging
450
        /// conflicts.
451
        /// </summary>
452
        /// <exception cref="InvalidOperationException">Thrown if the underlying value is not of type <see cref="RealmValueType.Int"/>.</exception>
453
        /// <returns>An 16-bit <see cref="RealmInteger{T}"/> representing the value stored in the database.</returns>
454
        /// <seealso cref="AsInt16"/>
455
        public RealmInteger<short> AsInt16RealmInteger() => AsRealmInteger(AsInt16());
242✔
456

457
        /// <summary>
458
        /// Returns the stored value as a <see cref="RealmInteger{T}"/>. It offers Increment/Decrement API that preserve intent when merging
459
        /// conflicts.
460
        /// </summary>
461
        /// <exception cref="InvalidOperationException">Thrown if the underlying value is not of type <see cref="RealmValueType.Int"/>.</exception>
462
        /// <returns>An 32-bit <see cref="RealmInteger{T}"/> representing the value stored in the database.</returns>
463
        /// <seealso cref="AsInt32"/>
464
        public RealmInteger<int> AsInt32RealmInteger() => AsRealmInteger(AsInt32());
244✔
465

466
        /// <summary>
467
        /// Returns the stored value as a <see cref="RealmInteger{T}"/>. It offers Increment/Decrement API that preserve intent when merging
468
        /// conflicts.
469
        /// </summary>
470
        /// <exception cref="InvalidOperationException">Thrown if the underlying value is not of type <see cref="RealmValueType.Int"/>.</exception>
471
        /// <returns>An 64-bit <see cref="RealmInteger{T}"/> representing the value stored in the database.</returns>
472
        /// <seealso cref="AsInt64"/>
473
        public RealmInteger<long> AsInt64RealmInteger() => AsRealmInteger(AsInt64());
242✔
474

475
        private RealmInteger<T> AsRealmInteger<T>(T value)
476
            where T : struct, IComparable<T>, IFormattable, IConvertible, IEquatable<T>
477
        {
478
            if (_objectHandle is null)
938✔
479
            {
480
                return new(value);
176✔
481
            }
482

483
            return new(value, _objectHandle, _propertyIndex);
762✔
484
        }
485

486
        /// <summary>
487
        /// Returns the stored value as an array of bytes.
488
        /// </summary>
489
        /// <exception cref="InvalidOperationException">Thrown if the underlying value is not of type <see cref="RealmValueType.Data"/>.</exception>
490
        /// <returns>An array of bytes representing the value stored in the database.</returns>
491
        public byte[] AsData()
492
        {
493
            EnsureType("byte[]", RealmValueType.Data);
3,094✔
494
            return _dataValue!;
3,094✔
495
        }
496

497
        /// <summary>
498
        /// Returns the stored value as a string.
499
        /// </summary>
500
        /// <exception cref="InvalidOperationException">Thrown if the underlying value is not of type <see cref="RealmValueType.String"/>.</exception>
501
        /// <returns> A string representing the value stored in the database.</returns>
502
        public string AsString()
503
        {
504
            EnsureType("string", RealmValueType.String);
7,245✔
505
            return _stringValue!;
7,244✔
506
        }
507

508
        /// <summary>
509
        /// Returns the stored value as a list.
510
        /// </summary>
511
        /// <exception cref="InvalidOperationException">Thrown if the underlying value is not of type <see cref="RealmValueType.List"/>.</exception>
512
        /// <returns> A list representing the value stored in the database.</returns>
513
        public IList<RealmValue> AsList()
514
        {
515
            EnsureType("List", RealmValueType.List);
462✔
516
            return _listValue!;
462✔
517
        }
518

519
        /// <summary>
520
        /// Returns the stored value as a dictionary.
521
        /// </summary>
522
        /// <exception cref="InvalidOperationException">Thrown if the underlying value is not of type <see cref="RealmValueType.Dictionary"/>.</exception>
523
        /// <returns> A dictionary representing the value stored in the database.</returns>
524
        public IDictionary<string, RealmValue> AsDictionary()
525
        {
526
            EnsureType("Dictionary", RealmValueType.Dictionary);
389✔
527
            return _dictionaryValue!;
389✔
528
        }
529

530
        /// <summary>
531
        /// Returns the stored value as a <see cref="RealmObjectBase"/>.
532
        /// </summary>
533
        /// <exception cref="InvalidOperationException">
534
        /// Thrown if the underlying value is not of type <see cref="RealmValueType.Object"/>.
535
        /// </exception>
536
        /// <returns>
537
        /// A <see cref="RealmObjectBase"/> instance representing the value stored in the database.
538
        /// </returns>
539
        public RealmObjectBase AsRealmObject() => AsRealmObject<RealmObjectBase>();
×
540

541
        /// <summary>
542
        /// Returns the stored value as a <see cref="IRealmObjectBase"/>.
543
        /// </summary>
544
        /// <exception cref="InvalidOperationException">
545
        /// Thrown if the underlying value is not of type <see cref="RealmValueType.Object"/>.
546
        /// </exception>
547
        /// <returns>
548
        /// A <see cref="IRealmObjectBase"/> instance representing the value stored in the database.
549
        /// </returns>
550
        public IRealmObjectBase AsIRealmObject() => AsRealmObject<IRealmObjectBase>();
14,200✔
551

552
        /// <summary>
553
        /// Returns the stored value as a <typeparamref name="T"/> which inherits from <see cref="RealmObjectBase"/>.
554
        /// </summary>
555
        /// <exception cref="InvalidOperationException">
556
        /// Thrown if the underlying value is not of type <see cref="RealmValueType.Object"/>.
557
        /// </exception>
558
        /// <typeparam name="T">The type of the object stored in the database.</typeparam>
559
        /// <returns>
560
        /// A <see cref="RealmObjectBase"/> instance representing the value stored in the database.
561
        /// </returns>
562
        public T AsRealmObject<T>()
563
            where T : IRealmObjectBase
564
        {
565
            EnsureType("object", RealmValueType.Object);
14,624✔
566
            return (T)_objectValue!;
14,624✔
567
        }
568

569
        /// <summary>
570
        /// Returns the stored value as a nullable <see cref="char"/>.
571
        /// </summary>
572
        /// <exception cref="InvalidOperationException">
573
        /// Thrown if the underlying value is not of type <see cref="RealmValueType.Int"/> or <see cref="RealmValueType.Null"/>.
574
        /// </exception>
575
        /// <returns>A nullable UTF-16 code unit representing the value stored in the database.</returns>
576
        public char? AsNullableChar() => Type == RealmValueType.Null ? null : AsChar();
101✔
577

578
        /// <summary>
579
        /// Returns the stored value as a nullable <see cref="byte"/>.
580
        /// </summary>
581
        /// <exception cref="InvalidOperationException">
582
        /// Thrown if the underlying value is not of type <see cref="RealmValueType.Int"/> or <see cref="RealmValueType.Null"/>.
583
        /// </exception>
584
        /// <returns>A nullable 8-bit unsigned integer representing the value stored in the database.</returns>
585
        /// <seealso cref="AsNullableByteRealmInteger"/>
586
        public byte? AsNullableByte() => Type == RealmValueType.Null ? null : AsByte();
189✔
587

588
        /// <summary>
589
        /// Returns the stored value as a nullable <see cref="short"/>.
590
        /// </summary>
591
        /// <exception cref="InvalidOperationException">
592
        /// Thrown if the underlying value is not of type <see cref="RealmValueType.Int"/> or <see cref="RealmValueType.Null"/>.
593
        /// </exception>
594
        /// <returns>A nullable 16-bit integer representing the value stored in the database.</returns>
595
        /// <seealso cref="AsNullableInt16RealmInteger"/>
596
        public short? AsNullableInt16() => Type == RealmValueType.Null ? null : AsInt16();
189✔
597

598
        /// <summary>
599
        /// Returns the stored value as a nullable <see cref="int"/>.
600
        /// </summary>
601
        /// <exception cref="InvalidOperationException">
602
        /// Thrown if the underlying value is not of type <see cref="RealmValueType.Int"/> or <see cref="RealmValueType.Null"/>.
603
        /// </exception>
604
        /// <returns>A nullable 32-bit integer representing the value stored in the database.</returns>
605
        /// <seealso cref="AsNullableInt32RealmInteger"/>
606
        public int? AsNullableInt32() => Type == RealmValueType.Null ? null : AsInt32();
194✔
607

608
        /// <summary>
609
        /// Returns the stored value as a nullable <see cref="long"/>.
610
        /// </summary>
611
        /// <exception cref="InvalidOperationException">
612
        /// Thrown if the underlying value is not of type <see cref="RealmValueType.Int"/> or <see cref="RealmValueType.Null"/>.
613
        /// </exception>
614
        /// <returns>A nullable 64-bit integer representing the value stored in the database.</returns>
615
        /// <seealso cref="AsNullableInt64RealmInteger"/>
616
        public long? AsNullableInt64() => Type == RealmValueType.Null ? null : AsInt64();
189✔
617

618
        /// <summary>
619
        /// Returns the stored value as a nullable <see cref="RealmInteger{T}"/>. It offers Increment/Decrement API that preserve intent when merging
620
        /// conflicts.
621
        /// </summary>
622
        /// <exception cref="InvalidOperationException">
623
        /// Thrown if the underlying value is not of type <see cref="RealmValueType.Int"/> or <see cref="RealmValueType.Null"/>.
624
        /// </exception>
625
        /// <returns>A nullable 8-bit <see cref="RealmInteger{T}"/> representing the value stored in the database.</returns>
626
        /// <seealso cref="AsNullableByte"/>
627
        public RealmInteger<byte>? AsNullableByteRealmInteger() => Type == RealmValueType.Null ? null : AsByteRealmInteger();
98✔
628

629
        /// <summary>
630
        /// Returns the stored value as a nullable <see cref="RealmInteger{T}"/>. It offers Increment/Decrement API that preserve intent when merging
631
        /// conflicts.
632
        /// </summary>
633
        /// <exception cref="InvalidOperationException">
634
        /// Thrown if the underlying value is not of type <see cref="RealmValueType.Int"/> or <see cref="RealmValueType.Null"/>.
635
        /// </exception>
636
        /// <returns>A nullable 16-bit <see cref="RealmInteger{T}"/> representing the value stored in the database.</returns>
637
        /// <seealso cref="AsNullableInt16"/>
638
        public RealmInteger<short>? AsNullableInt16RealmInteger() => Type == RealmValueType.Null ? null : AsInt16RealmInteger();
98✔
639

640
        /// <summary>
641
        /// Returns the stored value as a nullable <see cref="RealmInteger{T}"/>. It offers Increment/Decrement API that preserve intent when merging
642
        /// conflicts.
643
        /// </summary>
644
        /// <exception cref="InvalidOperationException">
645
        /// Thrown if the underlying value is not of type <see cref="RealmValueType.Int"/> or <see cref="RealmValueType.Null"/>.
646
        /// </exception>
647
        /// <returns>A nullable 32-bit <see cref="RealmInteger{T}"/> representing the value stored in the database.</returns>
648
        /// <seealso cref="AsNullableInt32"/>
649
        public RealmInteger<int>? AsNullableInt32RealmInteger() => Type == RealmValueType.Null ? null : AsInt32RealmInteger();
98✔
650

651
        /// <summary>
652
        /// Returns the stored value as a nullable <see cref="RealmInteger{T}"/>. It offers Increment/Decrement API that preserve intent when merging
653
        /// conflicts.
654
        /// </summary>
655
        /// <exception cref="InvalidOperationException">
656
        /// Thrown if the underlying value is not of type <see cref="RealmValueType.Int"/> or <see cref="RealmValueType.Null"/>.
657
        /// </exception>
658
        /// <returns>A nullable 64-bit <see cref="RealmInteger{T}"/> representing the value stored in the database.</returns>
659
        /// <seealso cref="AsNullableInt64"/>
660
        public RealmInteger<long>? AsNullableInt64RealmInteger() => Type == RealmValueType.Null ? null : AsInt64RealmInteger();
98✔
661

662
        /// <summary>
663
        /// Returns the stored value as a nullable <see cref="float"/>.
664
        /// </summary>
665
        /// <exception cref="InvalidOperationException">
666
        /// Thrown if the underlying value is not of type <see cref="RealmValueType.Float"/> or <see cref="RealmValueType.Null"/>.
667
        /// </exception>
668
        /// <returns>A nullable 32-bit floating point number representing the value stored in the database.</returns>
669
        public float? AsNullableFloat() => Type == RealmValueType.Null ? null : AsFloat();
117✔
670

671
        /// <summary>
672
        /// Returns the stored value as a nullable <see cref="double"/>.
673
        /// </summary>
674
        /// <exception cref="InvalidOperationException">
675
        /// Thrown if the underlying value is not of type <see cref="RealmValueType.Double"/> or <see cref="RealmValueType.Null"/>.
676
        /// </exception>
677
        /// <returns>A nullable 64-bit floating point number representing the value stored in the database.</returns>
678
        public double? AsNullableDouble() => Type == RealmValueType.Null ? null : AsDouble();
117✔
679

680
        /// <summary>
681
        /// Returns the stored value as a nullable <see cref="bool"/>.
682
        /// </summary>
683
        /// <exception cref="InvalidOperationException">
684
        /// Thrown if the underlying value is not of type <see cref="RealmValueType.Bool"/> or <see cref="RealmValueType.Null"/>.
685
        /// </exception>
686
        /// <returns>A nullable boolean representing the value stored in the database.</returns>
687
        public bool? AsNullableBool() => Type == RealmValueType.Null ? null : AsBool();
105✔
688

689
        /// <summary>
690
        /// Returns the stored value as a nullable <see cref="DateTimeOffset"/>.
691
        /// </summary>
692
        /// <exception cref="InvalidOperationException">
693
        /// Thrown if the underlying value is not of type <see cref="RealmValueType.Date"/> or <see cref="RealmValueType.Null"/>.
694
        /// </exception>
695
        /// <returns>A nullable DateTimeOffset value representing the value stored in the database.</returns>
696
        public DateTimeOffset? AsNullableDate() => Type == RealmValueType.Null ? null : AsDate();
109✔
697

698
        /// <summary>
699
        /// Returns the stored value as a nullable <see cref="decimal"/>.
700
        /// </summary>
701
        /// <exception cref="InvalidOperationException">
702
        /// Thrown if the underlying value is not of type <see cref="RealmValueType.Decimal128"/> or <see cref="RealmValueType.Null"/>.
703
        /// </exception>
704
        /// <returns>A nullable 96-bit decimal number representing the value stored in the database.</returns>
705
        public decimal? AsNullableDecimal() => Type == RealmValueType.Null ? null : AsDecimal();
119✔
706

707
        /// <summary>
708
        /// Returns the stored value as a nullable <see cref="Decimal128"/>.
709
        /// </summary>
710
        /// <exception cref="InvalidOperationException">
711
        /// Thrown if the underlying value is not of type <see cref="RealmValueType.Date"/> or <see cref="RealmValueType.Null"/>.
712
        /// </exception>
713
        /// <returns>A nullable 128-bit decimal number representing the value stored in the database.</returns>
714
        public Decimal128? AsNullableDecimal128() => Type == RealmValueType.Null ? null : AsDecimal128();
119✔
715

716
        /// <summary>
717
        /// Returns the stored value as a nullable <see cref="MongoDB.Bson.ObjectId"/>.
718
        /// </summary>
719
        /// <exception cref="InvalidOperationException">
720
        /// Thrown if the underlying value is not of type <see cref="RealmValueType.ObjectId"/> or <see cref="RealmValueType.Null"/>.
721
        /// </exception>
722
        /// <returns>A nullable ObjectId representing the value stored in the database.</returns>
723
        public ObjectId? AsNullableObjectId() => Type == RealmValueType.Null ? null : AsObjectId();
93✔
724

725
        /// <summary>
726
        /// Returns the stored value as a nullable <see cref="System.Guid"/>.
727
        /// </summary>
728
        /// <exception cref="InvalidOperationException">
729
        /// Thrown if the underlying value is not of type <see cref="RealmValueType.Guid"/> or <see cref="RealmValueType.Null"/>.
730
        /// </exception>
731
        /// <returns>A nullable Guid representing the value stored in the database.</returns>
732
        public Guid? AsNullableGuid() => Type == RealmValueType.Null ? null : AsGuid();
97✔
733

734
        /// <summary>
735
        /// Returns the stored value as an array of bytes.
736
        /// </summary>
737
        /// <exception cref="InvalidOperationException">
738
        /// Thrown if the underlying value is not of type <see cref="RealmValueType.Data"/> or <see cref="RealmValueType.Null"/>.
739
        /// </exception>
740
        /// <returns>
741
        /// A nullable array of bytes representing the value stored in the database.
742
        /// </returns>
743
        public byte[]? AsNullableData() => Type == RealmValueType.Null ? null : AsData();
152✔
744

745
        /// <summary>
746
        /// Returns the stored value as a string.
747
        /// </summary>
748
        /// <exception cref="InvalidOperationException">
749
        /// Thrown if the underlying value is not of type <see cref="RealmValueType.String"/> or <see cref="RealmValueType.Null"/>.
750
        /// </exception>
751
        /// <returns>
752
        /// A nullable string representing the value stored in the database.
753
        /// </returns>
754
        public string? AsNullableString() => Type == RealmValueType.Null ? null : AsString();
1,093✔
755

756
        /// <summary>
757
        /// Returns the stored value as a <see cref="RealmObjectBase"/>.
758
        /// </summary>
759
        /// <exception cref="InvalidOperationException">
760
        /// Thrown if the underlying value is not of type <see cref="RealmValueType.Object"/> or <see cref="RealmValueType.Null"/>.
761
        /// </exception>
762
        /// <returns>
763
        /// A nullable <see cref="RealmObjectBase"/> instance representing the value stored in the database. It will be <c>null</c> if <see cref="Type"/> is <see cref="RealmValueType.Null"/>.
764
        /// </returns>
765
        public RealmObjectBase? AsNullableRealmObject() => AsNullableRealmObject<RealmObjectBase>();
4✔
766

767
        /// <summary>
768
        /// Returns the stored value as a <see cref="IRealmObjectBase"/>.
769
        /// </summary>
770
        /// <exception cref="InvalidOperationException">
771
        /// Thrown if the underlying value is not of type <see cref="RealmValueType.Object"/> or <see cref="RealmValueType.Null"/>.
772
        /// </exception>
773
        /// <returns>
774
        /// A nullable <see cref="IRealmObjectBase"/> instance representing the value stored in the database. It will be <c>null</c> if <see cref="Type"/> is <see cref="RealmValueType.Null"/>.
775
        /// </returns>
776
        public IRealmObjectBase? AsNullableIRealmObject() => AsNullableRealmObject<IRealmObjectBase>();
2✔
777

778
        /// <summary>
779
        /// Returns the stored value as a <typeparamref name="T"/> which inherits from <see cref="RealmObjectBase"/>.
780
        /// </summary>
781
        /// <exception cref="InvalidOperationException">
782
        /// Thrown if the underlying value is not of type <see cref="RealmValueType.Object"/> or <see cref="RealmValueType.Null"/>.
783
        /// </exception>
784
        /// <typeparam name="T">The type of the object stored in the database.</typeparam>
785
        /// <returns>
786
        /// A nullable <see cref="RealmObjectBase"/> instance representing the value stored in the database. It will be <c>null</c> if <see cref="Type"/> is <see cref="RealmValueType.Null"/>.
787
        /// </returns>
788
        public T? AsNullableRealmObject<T>()
789
            where T : class, IRealmObjectBase
790
            => Type == RealmValueType.Null ? null : AsRealmObject<T>();
6!
791

792
        public T AsMappedObject<T>()
793
            where T : class, IMappedObject
794
        {
NEW
795
            EnsureType("dictionary", RealmValueType.Dictionary);
×
NEW
796
            throw new NotImplementedException();
×
797
        }
798

799
        public T? AsNullableMappedObject<T>()
800
            where T : class, IMappedObject
NEW
801
            => Type == RealmValueType.Null ? null : AsMappedObject<T>();
×
802

803
        /// <summary>
804
        /// Returns the stored value converted to <typeparamref name="T"/>.
805
        /// </summary>
806
        /// <typeparam name="T">The type to which to convert the value.</typeparam>
807
        /// <exception cref="InvalidCastException">Thrown if the type is not convertible to <typeparamref name="T"/>.</exception>
808
        /// <returns>The underlying value converted to <typeparamref name="T"/>.</returns>
809
        public T As<T>()
810
        {
811
            if (typeof(T) == typeof(RealmValue))
49,223✔
812
            {
813
                return Operator.Convert<RealmValue, T>(this);
4,643✔
814
            }
815

816
            if (typeof(IMappedObject).IsAssignableFrom(typeof(T)))
44,580✔
817
            {
818
                return Type switch
1!
819
                {
1✔
NEW
820
                    RealmValueType.Null => Operator.Convert<T>(null)!,
×
821
                    RealmValueType.Dictionary => throw new NotImplementedException(),
1✔
NEW
822
                    _ => throw new NotSupportedException($"Can't convert from {Type} to dictionary, which is the backing storage type for {typeof(T)}"),
×
823
                };
1✔
824
            }
825

826
            // This largely copies AsAny to avoid boxing the underlying value in an object
827
            return Type switch
44,579!
828
            {
44,579✔
829
                RealmValueType.Null => Operator.Convert<T>(null)!,
3,635✔
830
                RealmValueType.Int => Operator.Convert<long, T>(AsInt64()),
11,942✔
831
                RealmValueType.Bool => Operator.Convert<bool, T>(AsBool()),
1,304✔
832
                RealmValueType.String => Operator.Convert<string, T>(AsString()),
3,366✔
833
                RealmValueType.Data => Operator.Convert<byte[], T>(AsData()),
2,217✔
834
                RealmValueType.Date => Operator.Convert<DateTimeOffset, T>(AsDate()),
2,274✔
835
                RealmValueType.Float => Operator.Convert<float, T>(AsFloat()),
2,827✔
836
                RealmValueType.Double => Operator.Convert<double, T>(AsDouble()),
3,067✔
837
                RealmValueType.Decimal128 => Operator.Convert<Decimal128, T>(AsDecimal128()),
7,630✔
838
                RealmValueType.ObjectId => Operator.Convert<ObjectId, T>(AsObjectId()),
2,198✔
839
                RealmValueType.Guid => Operator.Convert<Guid, T>(AsGuid()),
224✔
840
                RealmValueType.Object => Operator.Convert<IRealmObjectBase, T>(AsIRealmObject()),
3,890✔
841
                RealmValueType.List => Operator.Convert<IEnumerable<RealmValue>, T>(AsList()),
2✔
842
                RealmValueType.Dictionary => Operator.Convert<IDictionary<string, RealmValue>, T>(AsDictionary()),
3✔
843
                _ => throw new NotSupportedException($"RealmValue of type {Type} is not supported."),
×
844
            };
44,579✔
845
        }
846

847
        /// <summary>
848
        /// Returns the stored value boxed in <see cref="object"/>.
849
        /// </summary>
850
        /// <returns>The underlying value.</returns>
851
        public object? AsAny()
852
        {
853
            return Type switch
2,038!
854
            {
2,038✔
855
                RealmValueType.Null => null,
125✔
856
                RealmValueType.Int => AsInt64(),
422✔
857
                RealmValueType.Bool => AsBool(),
70✔
858
                RealmValueType.String => AsString(),
757✔
859
                RealmValueType.Data => AsData(),
63✔
860
                RealmValueType.Date => AsDate(),
30✔
861
                RealmValueType.Float => AsFloat(),
57✔
862
                RealmValueType.Double => AsDouble(),
99✔
863
                RealmValueType.Decimal128 => AsDecimal128(),
125✔
864
                RealmValueType.ObjectId => AsObjectId(),
126✔
865
                RealmValueType.Guid => AsGuid(),
133✔
866
                RealmValueType.Object => AsIRealmObject(),
20✔
867
                RealmValueType.List => AsList(),
5✔
868
                RealmValueType.Dictionary => AsDictionary(),
6✔
869
                _ => throw new NotSupportedException($"RealmValue of type {Type} is not supported."),
×
870
            };
2,038✔
871
        }
872

873
        /// <summary>
874
        /// Gets the name of the type of the object contained in <see cref="RealmValue"/>.
875
        /// If it does not contain an object, it will return null.
876
        /// </summary>
877
        /// <returns>
878
        /// The name of the type stored in <see cref="RealmValue"/> if an object, null otherwise.
879
        /// </returns>
880
        public string? ObjectType
881
        {
882
            get
883
            {
884
                if (Type != RealmValueType.Object)
4✔
885
                {
886
                    return null;
2✔
887
                }
888

889
                var obj = AsIRealmObject();
2✔
890
                if (obj.IsManaged)
2✔
891
                {
892
                    return obj.ObjectSchema.Name;
1✔
893
                }
894

895
                return obj.GetType().Name;
1✔
896
            }
897
        }
898

899
        /// <summary>
900
        /// Returns the string representation of this <see cref="RealmValue"/>.
901
        /// </summary>
902
        /// <returns>A string describing the value.</returns>
903
        public override string ToString() => AsAny()?.ToString() ?? "<null>";
1,262✔
904

905
        /// <inheritdoc/>
906
        public override bool Equals(object? obj)
907
        {
908
            if (obj is not RealmValue val)
5!
909
            {
910
                return false;
×
911
            }
912

913
            return Equals(val);
5✔
914
        }
915

916
        /// <inheritdoc/>
917
        public override int GetHashCode()
918
        {
919
            unchecked
920
            {
921
                var hashCode = -1285871140;
2,487✔
922
                hashCode = (hashCode * -1521134295) + Type.GetHashCode();
2,487✔
923

924
                var valueHashCode = Type switch
2,487!
925
                {
2,487✔
926
                    RealmValueType.Int => AsInt64().GetHashCode(),
×
927
                    RealmValueType.Bool => AsBool().GetHashCode(),
372✔
928
                    RealmValueType.String => AsString().GetHashCode(),
413✔
929
                    RealmValueType.Data => AsData().Length,
234✔
930
                    RealmValueType.Date => AsDate().GetHashCode(),
231✔
931
                    RealmValueType.Float => AsFloat().GetHashCode(),
×
932
                    RealmValueType.Double => AsDouble().GetHashCode(),
×
933
                    RealmValueType.Decimal128 => AsDecimal128().GetHashCode(),
×
934
                    RealmValueType.ObjectId => AsObjectId().GetHashCode(),
223✔
935
                    RealmValueType.Object => AsIRealmObject().GetHashCode(),
229✔
936
                    RealmValueType.List => AsList().GetHashCode(),
×
937
                    RealmValueType.Dictionary => AsDictionary().GetHashCode(),
×
938
                    _ => 0,
785✔
939
                };
2,487✔
940

941
                hashCode = (hashCode * -1521134295) + valueHashCode;
2,487✔
942
                return hashCode;
2,487✔
943
            }
944
        }
945

946
        /// <summary>
947
        /// Converts a <see cref="RealmValue"/> to <see cref="char"/>. Equivalent to <see cref="AsChar"/>.
948
        /// </summary>
949
        /// <param name="val">The <see cref="RealmValue"/> to convert.</param>
950
        /// <returns>The <see cref="char"/> stored in the <see cref="RealmValue"/>.</returns>
951
        public static explicit operator char(RealmValue val) => val.AsChar();
159✔
952

953
        /// <summary>
954
        /// Converts a <see cref="RealmValue"/> to <see cref="byte"/>. Equivalent to <see cref="AsByte"/>.
955
        /// </summary>
956
        /// <param name="val">The <see cref="RealmValue"/> to convert.</param>
957
        /// <returns>The <see cref="byte"/> stored in the <see cref="RealmValue"/>.</returns>
958
        public static explicit operator byte(RealmValue val) => val.AsByte();
198✔
959

960
        /// <summary>
961
        /// Converts a <see cref="RealmValue"/> to <see cref="short"/>. Equivalent to <see cref="AsInt16"/>.
962
        /// </summary>
963
        /// <param name="val">The <see cref="RealmValue"/> to convert.</param>
964
        /// <returns>The <see cref="short"/> stored in the <see cref="RealmValue"/>.</returns>
965
        public static explicit operator short(RealmValue val) => val.AsInt16();
194✔
966

967
        /// <summary>
968
        /// Converts a <see cref="RealmValue"/> to <see cref="int"/>. Equivalent to <see cref="AsInt32"/>.
969
        /// </summary>
970
        /// <param name="val">The <see cref="RealmValue"/> to convert.</param>
971
        /// <returns>The <see cref="int"/> stored in the <see cref="RealmValue"/>.</returns>
972
        public static explicit operator int(RealmValue val) => val.AsInt32();
1,311✔
973

974
        /// <summary>
975
        /// Converts a <see cref="RealmValue"/> to <see cref="long"/>. Equivalent to <see cref="AsInt64"/>.
976
        /// </summary>
977
        /// <param name="val">The <see cref="RealmValue"/> to convert.</param>
978
        /// <returns>The <see cref="long"/> stored in the <see cref="RealmValue"/>.</returns>
979
        public static explicit operator long(RealmValue val) => val.AsInt64();
187✔
980

981
        /// <summary>
982
        /// Converts a <see cref="RealmValue"/> to <see cref="float"/>. Equivalent to <see cref="AsFloat"/>.
983
        /// </summary>
984
        /// <param name="val">The <see cref="RealmValue"/> to convert.</param>
985
        /// <returns>The <see cref="float"/> stored in the <see cref="RealmValue"/>.</returns>
986
        public static explicit operator float(RealmValue val) => val.AsFloat();
159✔
987

988
        /// <summary>
989
        /// Converts a <see cref="RealmValue"/> to <see cref="double"/>. Equivalent to <see cref="AsDouble"/>.
990
        /// </summary>
991
        /// <param name="val">The <see cref="RealmValue"/> to convert.</param>
992
        /// <returns>The <see cref="double"/> stored in the <see cref="RealmValue"/>.</returns>
993
        public static explicit operator double(RealmValue val) => val.AsDouble();
209✔
994

995
        /// <summary>
996
        /// Converts a <see cref="RealmValue"/> to <see cref="bool"/>. Equivalent to <see cref="AsBool"/>.
997
        /// </summary>
998
        /// <param name="val">The <see cref="RealmValue"/> to convert.</param>
999
        /// <returns>The <see cref="bool"/> stored in the <see cref="RealmValue"/>.</returns>
1000
        public static explicit operator bool(RealmValue val) => val.AsBool();
167✔
1001

1002
        /// <summary>
1003
        /// Converts a <see cref="RealmValue"/> to <see cref="DateTimeOffset"/>. Equivalent to <see cref="AsDate"/>.
1004
        /// </summary>
1005
        /// <param name="val">The <see cref="RealmValue"/> to convert.</param>
1006
        /// <returns>The <see cref="DateTimeOffset"/> stored in the <see cref="RealmValue"/>.</returns>
1007
        public static explicit operator DateTimeOffset(RealmValue val) => val.AsDate();
332✔
1008

1009
        /// <summary>
1010
        /// Converts a <see cref="RealmValue"/> to <see cref="decimal"/>. Equivalent to <see cref="AsDecimal"/>.
1011
        /// </summary>
1012
        /// <param name="val">The <see cref="RealmValue"/> to convert.</param>
1013
        /// <returns>The <see cref="decimal"/> stored in the <see cref="RealmValue"/>.</returns>
1014
        public static explicit operator decimal(RealmValue val) => val.AsDecimal();
173✔
1015

1016
        /// <summary>
1017
        /// Converts a <see cref="RealmValue"/> to <see cref="Decimal128"/>. Equivalent to <see cref="AsDecimal128"/>.
1018
        /// </summary>
1019
        /// <param name="val">The <see cref="RealmValue"/> to convert.</param>
1020
        /// <returns>The <see cref="Decimal128"/> stored in the <see cref="RealmValue"/>.</returns>
1021
        public static explicit operator Decimal128(RealmValue val) => val.AsDecimal128();
175✔
1022

1023
        /// <summary>
1024
        /// Converts a <see cref="RealmValue"/> to <see cref="MongoDB.Bson.ObjectId"/>. Equivalent to <see cref="AsObjectId"/>.
1025
        /// </summary>
1026
        /// <param name="val">The <see cref="RealmValue"/> to convert.</param>
1027
        /// <returns>The <see cref="MongoDB.Bson.ObjectId"/> stored in the <see cref="RealmValue"/>.</returns>
1028
        public static explicit operator ObjectId(RealmValue val) => val.AsObjectId();
340✔
1029

1030
        /// <summary>
1031
        /// Converts a <see cref="RealmValue"/> to <see cref="System.Guid"/>. Equivalent to <see cref="AsGuid"/>.
1032
        /// </summary>
1033
        /// <param name="val">The <see cref="RealmValue"/> to convert.</param>
1034
        /// <returns>The <see cref="System.Guid"/> stored in the <see cref="RealmValue"/>.</returns>
1035
        public static explicit operator Guid(RealmValue val) => val.AsGuid();
223✔
1036

1037
        /// <summary>
1038
        /// Converts a <see cref="RealmValue"/> to <see cref="char">char?</see>. Equivalent to <see cref="AsNullableChar"/>.
1039
        /// </summary>
1040
        /// <param name="val">The <see cref="RealmValue"/> to convert.</param>
1041
        /// <returns>The <see cref="char">char?</see> stored in the <see cref="RealmValue"/>.</returns>
1042
        public static explicit operator char?(RealmValue val) => val.AsNullableChar();
99✔
1043

1044
        /// <summary>
1045
        /// Converts a <see cref="RealmValue"/> to <see cref="byte">byte?</see>. Equivalent to <see cref="AsNullableByte"/>.
1046
        /// </summary>
1047
        /// <param name="val">The <see cref="RealmValue"/> to convert.</param>
1048
        /// <returns>The <see cref="byte">byte?</see> stored in the <see cref="RealmValue"/>.</returns>
1049
        public static explicit operator byte?(RealmValue val) => val.AsNullableByte();
143✔
1050

1051
        /// <summary>
1052
        /// Converts a <see cref="RealmValue"/> to <see cref="short">short?</see>. Equivalent to <see cref="AsNullableInt16"/>.
1053
        /// </summary>
1054
        /// <param name="val">The <see cref="RealmValue"/> to convert.</param>
1055
        /// <returns>The <see cref="short">short?</see> stored in the <see cref="RealmValue"/>.</returns>
1056
        public static explicit operator short?(RealmValue val) => val.AsNullableInt16();
143✔
1057

1058
        /// <summary>
1059
        /// Converts a <see cref="RealmValue"/> to <see cref="int">int?</see>. Equivalent to <see cref="AsNullableInt32"/>.
1060
        /// </summary>
1061
        /// <param name="val">The <see cref="RealmValue"/> to convert.</param>
1062
        /// <returns>The <see cref="int">int?</see> stored in the <see cref="RealmValue"/>.</returns>
1063
        public static explicit operator int?(RealmValue val) => val.AsNullableInt32();
148✔
1064

1065
        /// <summary>
1066
        /// Converts a <see cref="RealmValue"/> to <see cref="long">long?</see>. Equivalent to <see cref="AsNullableInt64"/>.
1067
        /// </summary>
1068
        /// <param name="val">The <see cref="RealmValue"/> to convert.</param>
1069
        /// <returns>The <see cref="long">long?</see> stored in the <see cref="RealmValue"/>.</returns>
1070
        public static explicit operator long?(RealmValue val) => val.AsNullableInt64();
143✔
1071

1072
        /// <summary>
1073
        /// Converts a <see cref="RealmValue"/> to <see cref="float">float?</see>. Equivalent to <see cref="AsNullableFloat"/>.
1074
        /// </summary>
1075
        /// <param name="val">The <see cref="RealmValue"/> to convert.</param>
1076
        /// <returns>The <see cref="float">float?</see> stored in the <see cref="RealmValue"/>.</returns>
1077
        public static explicit operator float?(RealmValue val) => val.AsNullableFloat();
105✔
1078

1079
        /// <summary>
1080
        /// Converts a <see cref="RealmValue"/> to <see cref="double">double?</see>. Equivalent to <see cref="AsNullableDouble"/>.
1081
        /// </summary>
1082
        /// <param name="val">The <see cref="RealmValue"/> to convert.</param>
1083
        /// <returns>The <see cref="double">double?</see> stored in the <see cref="RealmValue"/>.</returns>
1084
        public static explicit operator double?(RealmValue val) => val.AsNullableDouble();
105✔
1085

1086
        /// <summary>
1087
        /// Converts a <see cref="RealmValue"/> to <see cref="bool">bool?</see>. Equivalent to <see cref="AsNullableBool"/>.
1088
        /// </summary>
1089
        /// <param name="val">The <see cref="RealmValue"/> to convert.</param>
1090
        /// <returns>The <see cref="bool">bool?</see> stored in the <see cref="RealmValue"/>.</returns>
1091
        public static explicit operator bool?(RealmValue val) => val.AsNullableBool();
99✔
1092

1093
        /// <summary>
1094
        /// Converts a <see cref="RealmValue"/> to <see cref="DateTimeOffset">DateTimeOffset?</see>. Equivalent to <see cref="AsNullableDate"/>.
1095
        /// </summary>
1096
        /// <param name="val">The <see cref="RealmValue"/> to convert.</param>
1097
        /// <returns>The <see cref="DateTimeOffset">DateTimeOffset?</see> stored in the <see cref="RealmValue"/>.</returns>
1098
        public static explicit operator DateTimeOffset?(RealmValue val) => val.AsNullableDate();
101✔
1099

1100
        /// <summary>
1101
        /// Converts a <see cref="RealmValue"/> to <see cref="decimal">decimal?</see>. Equivalent to <see cref="AsNullableDecimal"/>.
1102
        /// </summary>
1103
        /// <param name="val">The <see cref="RealmValue"/> to convert.</param>
1104
        /// <returns>The <see cref="decimal">decimal?</see> stored in the <see cref="RealmValue"/>.</returns>
1105
        public static explicit operator decimal?(RealmValue val) => val.AsNullableDecimal();
107✔
1106

1107
        /// <summary>
1108
        /// Converts a <see cref="RealmValue"/> to <see cref="Decimal128">Decimal128?</see>. Equivalent to <see cref="AsNullableDecimal128"/>.
1109
        /// </summary>
1110
        /// <param name="val">The <see cref="RealmValue"/> to convert.</param>
1111
        /// <returns>The <see cref="Decimal128">Decimal128?</see> stored in the <see cref="RealmValue"/>.</returns>
1112
        public static explicit operator Decimal128?(RealmValue val) => val.AsNullableDecimal128();
107✔
1113

1114
        /// <summary>
1115
        /// Converts a <see cref="RealmValue"/> to <see cref="MongoDB.Bson.ObjectId">ObjectId?</see>. Equivalent to <see cref="AsNullableObjectId"/>.
1116
        /// </summary>
1117
        /// <param name="val">The <see cref="RealmValue"/> to convert.</param>
1118
        /// <returns>The <see cref="MongoDB.Bson.ObjectId">ObjectId?</see> stored in the <see cref="RealmValue"/>.</returns>
1119
        public static explicit operator ObjectId?(RealmValue val) => val.AsNullableObjectId();
87✔
1120

1121
        /// <summary>
1122
        /// Converts a <see cref="RealmValue"/> to <see cref="System.Guid">Guid?</see>. Equivalent to <see cref="AsNullableGuid"/>.
1123
        /// </summary>
1124
        /// <param name="val">The <see cref="RealmValue"/> to convert.</param>
1125
        /// <returns>The <see cref="System.Guid">Guid?</see> stored in the <see cref="RealmValue"/>.</returns>
1126
        public static explicit operator Guid?(RealmValue val) => val.AsNullableGuid();
91✔
1127

1128
        /// <summary>
1129
        /// Converts a <see cref="RealmValue"/> to <see cref="RealmInteger{T}">RealmInteger&lt;byte&gt;</see>.
1130
        /// Equivalent to <see cref="AsByteRealmInteger"/>.
1131
        /// </summary>
1132
        /// <param name="val">The <see cref="RealmValue"/> to convert.</param>
1133
        /// <returns>The <see cref="RealmInteger{T}">RealmInteger&lt;byte&gt;</see> stored in the <see cref="RealmValue"/>.</returns>
1134
        public static explicit operator RealmInteger<byte>(RealmValue val) => val.AsByteRealmInteger();
115✔
1135

1136
        /// <summary>
1137
        /// Converts a <see cref="RealmValue"/> to <see cref="RealmInteger{T}">RealmInteger&lt;short&gt;</see>.
1138
        /// Equivalent to <see cref="AsInt16RealmInteger"/>.
1139
        /// </summary>
1140
        /// <param name="val">The <see cref="RealmValue"/> to convert.</param>
1141
        /// <returns>The <see cref="RealmInteger{T}">RealmInteger&lt;short&gt;</see> stored in the <see cref="RealmValue"/>.</returns>
1142
        public static explicit operator RealmInteger<short>(RealmValue val) => val.AsInt16RealmInteger();
147✔
1143

1144
        /// <summary>
1145
        /// Converts a <see cref="RealmValue"/> to <see cref="RealmInteger{T}">RealmInteger&lt;int&gt;</see>.
1146
        /// Equivalent to <see cref="AsInt32RealmInteger"/>.
1147
        /// </summary>
1148
        /// <param name="val">The <see cref="RealmValue"/> to convert.</param>
1149
        /// <returns>The <see cref="RealmInteger{T}">RealmInteger&lt;int&gt;</see> stored in the <see cref="RealmValue"/>.</returns>
1150
        public static explicit operator RealmInteger<int>(RealmValue val) => val.AsInt32RealmInteger();
147✔
1151

1152
        /// <summary>
1153
        /// Converts a <see cref="RealmValue"/> to <see cref="RealmInteger{T}">RealmInteger&lt;long&gt;</see>.
1154
        /// Equivalent to <see cref="AsInt64RealmInteger"/>.
1155
        /// </summary>
1156
        /// <param name="val">The <see cref="RealmValue"/> to convert.</param>
1157
        /// <returns>The <see cref="RealmInteger{T}">RealmInteger&lt;long&gt;</see> stored in the <see cref="RealmValue"/>.</returns>
1158
        public static explicit operator RealmInteger<long>(RealmValue val) => val.AsInt64RealmInteger();
147✔
1159

1160
        /// <summary>
1161
        /// Converts a <see cref="RealmValue"/> to <see cref="RealmInteger{T}">RealmInteger&lt;byte&gt;?</see>.
1162
        /// Equivalent to <see cref="AsNullableByteRealmInteger"/>.
1163
        /// </summary>
1164
        /// <param name="val">The <see cref="RealmValue"/> to convert.</param>
1165
        /// <returns>The <see cref="RealmInteger{T}">RealmInteger&lt;byte&gt;?</see> stored in the <see cref="RealmValue"/>.</returns>
1166
        public static explicit operator RealmInteger<byte>?(RealmValue val) => val.AsNullableByteRealmInteger();
52✔
1167

1168
        /// <summary>
1169
        /// Converts a <see cref="RealmValue"/> to <see cref="RealmInteger{T}">RealmInteger&lt;short&gt;?</see>.
1170
        /// Equivalent to <see cref="AsNullableInt16RealmInteger"/>.
1171
        /// </summary>
1172
        /// <param name="val">The <see cref="RealmValue"/> to convert.</param>
1173
        /// <returns>The <see cref="RealmInteger{T}">RealmInteger&lt;short&gt;?</see> stored in the <see cref="RealmValue"/>.</returns>
1174
        public static explicit operator RealmInteger<short>?(RealmValue val) => val.AsNullableInt16RealmInteger();
52✔
1175

1176
        /// <summary>
1177
        /// Converts a <see cref="RealmValue"/> to <see cref="RealmInteger{T}">RealmInteger&lt;int&gt;?</see>.
1178
        /// Equivalent to <see cref="AsNullableInt32RealmInteger"/>.
1179
        /// </summary>
1180
        /// <param name="val">The <see cref="RealmValue"/> to convert.</param>
1181
        /// <returns>The <see cref="RealmInteger{T}">RealmInteger&lt;int&gt;?</see> stored in the <see cref="RealmValue"/>.</returns>
1182
        public static explicit operator RealmInteger<int>?(RealmValue val) => val.AsNullableInt32RealmInteger();
52✔
1183

1184
        /// <summary>
1185
        /// Converts a <see cref="RealmValue"/> to <see cref="RealmInteger{T}">RealmInteger&lt;long&gt;?</see>.
1186
        /// Equivalent to <see cref="AsNullableInt64RealmInteger"/>.
1187
        /// </summary>
1188
        /// <param name="val">The <see cref="RealmValue"/> to convert.</param>
1189
        /// <returns>The <see cref="RealmInteger{T}">RealmInteger&lt;long&gt;?</see> stored in the <see cref="RealmValue"/>.</returns>
1190
        public static explicit operator RealmInteger<long>?(RealmValue val) => val.AsNullableInt64RealmInteger();
52✔
1191

1192
        /// <summary>
1193
        /// Converts a <see cref="RealmValue"/> to <see cref="byte">byte[]?</see>. Equivalent to <see cref="AsNullableData"/>.
1194
        /// </summary>
1195
        /// <param name="val">The <see cref="RealmValue"/> to convert.</param>
1196
        /// <returns>The <see cref="byte">byte[]?</see> stored in the <see cref="RealmValue"/>.</returns>
1197
        public static explicit operator byte[]?(RealmValue val) => val.AsNullableData();
146✔
1198

1199
        /// <summary>
1200
        /// Converts a <see cref="RealmValue"/> to <see cref="string">string?</see>. Equivalent to <see cref="AsNullableString"/>.
1201
        /// </summary>
1202
        /// <param name="val">The <see cref="RealmValue"/> to convert.</param>
1203
        /// <returns>The <see cref="string">string?</see> stored in the <see cref="RealmValue"/>.</returns>
1204
        public static explicit operator string?(RealmValue val) => val.AsNullableString();
1,085✔
1205

1206
        /// <summary>
1207
        /// Converts a <see cref="RealmValue"/> to <see cref="RealmObjectBase">RealmObjectBase?</see>. Equivalent to <see cref="AsNullableRealmObject"/>.
1208
        /// </summary>
1209
        /// <param name="val">The <see cref="RealmValue"/> to convert.</param>
1210
        /// <returns>The <see cref="RealmObjectBase">RealmObjectBase?</see> stored in the <see cref="RealmValue"/>.</returns>
1211
        public static explicit operator RealmObjectBase?(RealmValue val) => val.AsNullableRealmObject();
2✔
1212

1213
        /// <summary>
1214
        /// Implicitly constructs a <see cref="RealmValue"/> from <see cref="char"/>.
1215
        /// </summary>
1216
        /// <param name="val">The value to store in the <see cref="RealmValue"/>.</param>
1217
        /// <returns>A <see cref="RealmValue"/> containing the supplied <paramref name="val"/>.</returns>
1218
        public static implicit operator RealmValue(char val) => Int(val);
278✔
1219

1220
        /// <summary>
1221
        /// Implicitly constructs a <see cref="RealmValue"/> from <see cref="byte"/>.
1222
        /// </summary>
1223
        /// <param name="val">The value to store in the <see cref="RealmValue"/>.</param>
1224
        /// <returns>A <see cref="RealmValue"/> containing the supplied <paramref name="val"/>.</returns>
1225
        public static implicit operator RealmValue(byte val) => Int(val);
2,118✔
1226

1227
        /// <summary>
1228
        /// Implicitly constructs a <see cref="RealmValue"/> from <see cref="short"/>.
1229
        /// </summary>
1230
        /// <param name="val">The value to store in the <see cref="RealmValue"/>.</param>
1231
        /// <returns>A <see cref="RealmValue"/> containing the supplied <paramref name="val"/>.</returns>
1232
        public static implicit operator RealmValue(short val) => Int(val);
2,140✔
1233

1234
        /// <summary>
1235
        /// Implicitly constructs a <see cref="RealmValue"/> from <see cref="int"/>.
1236
        /// </summary>
1237
        /// <param name="val">The value to store in the <see cref="RealmValue"/>.</param>
1238
        /// <returns>A <see cref="RealmValue"/> containing the supplied <paramref name="val"/>.</returns>
1239
        public static implicit operator RealmValue(int val) => Int(val);
5,234✔
1240

1241
        /// <summary>
1242
        /// Implicitly constructs a <see cref="RealmValue"/> from <see cref="long"/>.
1243
        /// </summary>
1244
        /// <param name="val">The value to store in the <see cref="RealmValue"/>.</param>
1245
        /// <returns>A <see cref="RealmValue"/> containing the supplied <paramref name="val"/>.</returns>
1246
        public static implicit operator RealmValue(long val) => Int(val);
3,254✔
1247

1248
        /// <summary>
1249
        /// Implicitly constructs a <see cref="RealmValue"/> from <see cref="float"/>.
1250
        /// </summary>
1251
        /// <param name="val">The value to store in the <see cref="RealmValue"/>.</param>
1252
        /// <returns>A <see cref="RealmValue"/> containing the supplied <paramref name="val"/>.</returns>
1253
        public static implicit operator RealmValue(float val) => Float(val);
2,682✔
1254

1255
        /// <summary>
1256
        /// Implicitly constructs a <see cref="RealmValue"/> from <see cref="double"/>.
1257
        /// </summary>
1258
        /// <param name="val">The value to store in the <see cref="RealmValue"/>.</param>
1259
        /// <returns>A <see cref="RealmValue"/> containing the supplied <paramref name="val"/>.</returns>
1260
        public static implicit operator RealmValue(double val) => Double(val);
3,793✔
1261

1262
        /// <summary>
1263
        /// Implicitly constructs a <see cref="RealmValue"/> from <see cref="bool"/>.
1264
        /// </summary>
1265
        /// <param name="val">The value to store in the <see cref="RealmValue"/>.</param>
1266
        /// <returns>A <see cref="RealmValue"/> containing the supplied <paramref name="val"/>.</returns>
1267
        public static implicit operator RealmValue(bool val) => Bool(val);
1,918✔
1268

1269
        /// <summary>
1270
        /// Implicitly constructs a <see cref="RealmValue"/> from <see cref="DateTimeOffset"/>.
1271
        /// </summary>
1272
        /// <param name="val">The value to store in the <see cref="RealmValue"/>.</param>
1273
        /// <returns>A <see cref="RealmValue"/> containing the supplied <paramref name="val"/>.</returns>
1274
        public static implicit operator RealmValue(DateTimeOffset val) => Date(val);
3,531✔
1275

1276
        /// <summary>
1277
        /// Implicitly constructs a <see cref="RealmValue"/> from <see cref="decimal"/>.
1278
        /// </summary>
1279
        /// <param name="val">The value to store in the <see cref="RealmValue"/>.</param>
1280
        /// <returns>A <see cref="RealmValue"/> containing the supplied <paramref name="val"/>.</returns>
1281
        public static implicit operator RealmValue(decimal val) => Decimal(val);
2,482✔
1282

1283
        /// <summary>
1284
        /// Implicitly constructs a <see cref="RealmValue"/> from <see cref="Decimal128"/>.
1285
        /// </summary>
1286
        /// <param name="val">The value to store in the <see cref="RealmValue"/>.</param>
1287
        /// <returns>A <see cref="RealmValue"/> containing the supplied <paramref name="val"/>.</returns>
1288
        public static implicit operator RealmValue(Decimal128 val) => Decimal(val);
2,538✔
1289

1290
        /// <summary>
1291
        /// Implicitly constructs a <see cref="RealmValue"/> from <see cref="MongoDB.Bson.ObjectId"/>.
1292
        /// </summary>
1293
        /// <param name="val">The value to store in the <see cref="RealmValue"/>.</param>
1294
        /// <returns>A <see cref="RealmValue"/> containing the supplied <paramref name="val"/>.</returns>
1295
        public static implicit operator RealmValue(ObjectId val) => ObjectId(val);
8,712✔
1296

1297
        /// <summary>
1298
        /// Implicitly constructs a <see cref="RealmValue"/> from <see cref="System.Guid"/>.
1299
        /// </summary>
1300
        /// <param name="val">The value to store in the <see cref="RealmValue"/>.</param>
1301
        /// <returns>A <see cref="RealmValue"/> containing the supplied <paramref name="val"/>.</returns>
1302
        public static implicit operator RealmValue(Guid val) => Guid(val);
715✔
1303

1304
        /// <summary>
1305
        /// Implicitly constructs a <see cref="RealmValue"/> from <see cref="char">char?</see>.
1306
        /// </summary>
1307
        /// <param name="val">The value to store in the <see cref="RealmValue"/>.</param>
1308
        /// <returns>A <see cref="RealmValue"/> containing the supplied <paramref name="val"/>.</returns>
1309
        public static implicit operator RealmValue(char? val) => val == null ? Null : Int(val.Value);
201✔
1310

1311
        /// <summary>
1312
        /// Implicitly constructs a <see cref="RealmValue"/> from <see cref="byte">byte?</see>.
1313
        /// </summary>
1314
        /// <param name="val">The value to store in the <see cref="RealmValue"/>.</param>
1315
        /// <returns>A <see cref="RealmValue"/> containing the supplied <paramref name="val"/>.</returns>
1316
        public static implicit operator RealmValue(byte? val) => val == null ? Null : Int(val.Value);
2,243✔
1317

1318
        /// <summary>
1319
        /// Implicitly constructs a <see cref="RealmValue"/> from <see cref="short">short?</see>.
1320
        /// </summary>
1321
        /// <param name="val">The value to store in the <see cref="RealmValue"/>.</param>
1322
        /// <returns>A <see cref="RealmValue"/> containing the supplied <paramref name="val"/>.</returns>
1323
        public static implicit operator RealmValue(short? val) => val == null ? Null : Int(val.Value);
2,262✔
1324

1325
        /// <summary>
1326
        /// Implicitly constructs a <see cref="RealmValue"/> from <see cref="int">int?</see>.
1327
        /// </summary>
1328
        /// <param name="val">The value to store in the <see cref="RealmValue"/>.</param>
1329
        /// <returns>A <see cref="RealmValue"/> containing the supplied <paramref name="val"/>.</returns>
1330
        public static implicit operator RealmValue(int? val) => val == null ? Null : Int(val.Value);
2,262✔
1331

1332
        /// <summary>
1333
        /// Implicitly constructs a <see cref="RealmValue"/> from <see cref="long">long?</see>.
1334
        /// </summary>
1335
        /// <param name="val">The value to store in the <see cref="RealmValue"/>.</param>
1336
        /// <returns>A <see cref="RealmValue"/> containing the supplied <paramref name="val"/>.</returns>
1337
        public static implicit operator RealmValue(long? val) => val == null ? Null : Int(val.Value);
2,725✔
1338

1339
        /// <summary>
1340
        /// Implicitly constructs a <see cref="RealmValue"/> from <see cref="float">float?</see>.
1341
        /// </summary>
1342
        /// <param name="val">The value to store in the <see cref="RealmValue"/>.</param>
1343
        /// <returns>A <see cref="RealmValue"/> containing the supplied <paramref name="val"/>.</returns>
1344
        public static implicit operator RealmValue(float? val) => val == null ? Null : Float(val.Value);
2,131✔
1345

1346
        /// <summary>
1347
        /// Implicitly constructs a <see cref="RealmValue"/> from <see cref="double">double?</see>.
1348
        /// </summary>
1349
        /// <param name="val">The value to store in the <see cref="RealmValue"/>.</param>
1350
        /// <returns>A <see cref="RealmValue"/> containing the supplied <paramref name="val"/>.</returns>
1351
        public static implicit operator RealmValue(double? val) => val == null ? Null : Double(val.Value);
2,250✔
1352

1353
        /// <summary>
1354
        /// Implicitly constructs a <see cref="RealmValue"/> from <see cref="bool">bool?</see>.
1355
        /// </summary>
1356
        /// <param name="val">The value to store in the <see cref="RealmValue"/>.</param>
1357
        /// <returns>A <see cref="RealmValue"/> containing the supplied <paramref name="val"/>.</returns>
1358
        public static implicit operator RealmValue(bool? val) => val == null ? Null : Bool(val.Value);
2,019✔
1359

1360
        /// <summary>
1361
        /// Implicitly constructs a <see cref="RealmValue"/> from <see cref="DateTimeOffset">DateTimeOffset?</see>.
1362
        /// </summary>
1363
        /// <param name="val">The value to store in the <see cref="RealmValue"/>.</param>
1364
        /// <returns>A <see cref="RealmValue"/> containing the supplied <paramref name="val"/>.</returns>
1365
        public static implicit operator RealmValue(DateTimeOffset? val) => val == null ? Null : Date(val.Value);
2,518✔
1366

1367
        /// <summary>
1368
        /// Implicitly constructs a <see cref="RealmValue"/> from <see cref="decimal">decimal?</see>.
1369
        /// </summary>
1370
        /// <param name="val">The value to store in the <see cref="RealmValue"/>.</param>
1371
        /// <returns>A <see cref="RealmValue"/> containing the supplied <paramref name="val"/>.</returns>
1372
        public static implicit operator RealmValue(decimal? val) => val == null ? Null : Decimal(val.Value);
2,439✔
1373

1374
        /// <summary>
1375
        /// Implicitly constructs a <see cref="RealmValue"/> from <see cref="Decimal128">Decimal128?</see>.
1376
        /// </summary>
1377
        /// <param name="val">The value to store in the <see cref="RealmValue"/>.</param>
1378
        /// <returns>A <see cref="RealmValue"/> containing the supplied <paramref name="val"/>.</returns>
1379
        public static implicit operator RealmValue(Decimal128? val) => val == null ? Null : Decimal(val.Value);
2,491✔
1380

1381
        /// <summary>
1382
        /// Implicitly constructs a <see cref="RealmValue"/> from <see cref="MongoDB.Bson.ObjectId">ObjectId?</see>.
1383
        /// </summary>
1384
        /// <param name="val">The value to store in the <see cref="RealmValue"/>.</param>
1385
        /// <returns>A <see cref="RealmValue"/> containing the supplied <paramref name="val"/>.</returns>
1386
        public static implicit operator RealmValue(ObjectId? val) => val == null ? Null : ObjectId(val.Value);
2,181✔
1387

1388
        /// <summary>
1389
        /// Implicitly constructs a <see cref="RealmValue"/> from <see cref="System.Guid">Guid?</see>.
1390
        /// </summary>
1391
        /// <param name="val">The value to store in the <see cref="RealmValue"/>.</param>
1392
        /// <returns>A <see cref="RealmValue"/> containing the supplied <paramref name="val"/>.</returns>
1393
        public static implicit operator RealmValue(Guid? val) => val == null ? Null : Guid(val.Value);
201✔
1394

1395
        /// <summary>
1396
        /// Implicitly constructs a <see cref="RealmValue"/> from <see cref="RealmInteger{T}">RealmInteger&lt;byte&gt;</see>.
1397
        /// </summary>
1398
        /// <param name="val">The value to store in the <see cref="RealmValue"/>.</param>
1399
        /// <returns>A <see cref="RealmValue"/> containing the supplied <paramref name="val"/>.</returns>
1400
        public static implicit operator RealmValue(RealmInteger<byte> val) => Int(val);
85✔
1401

1402
        /// <summary>
1403
        /// Implicitly constructs a <see cref="RealmValue"/> from <see cref="RealmInteger{T}">RealmInteger&lt;short&gt;</see>.
1404
        /// </summary>
1405
        /// <param name="val">The value to store in the <see cref="RealmValue"/>.</param>
1406
        /// <returns>A <see cref="RealmValue"/> containing the supplied <paramref name="val"/>.</returns>
1407
        public static implicit operator RealmValue(RealmInteger<short> val) => Int(val);
101✔
1408

1409
        /// <summary>
1410
        /// Implicitly constructs a <see cref="RealmValue"/> from <see cref="RealmInteger{T}">RealmInteger&lt;int&gt;</see>.
1411
        /// </summary>
1412
        /// <param name="val">The value to store in the <see cref="RealmValue"/>.</param>
1413
        /// <returns>A <see cref="RealmValue"/> containing the supplied <paramref name="val"/>.</returns>
1414
        public static implicit operator RealmValue(RealmInteger<int> val) => Int(val);
101✔
1415

1416
        /// <summary>
1417
        /// Implicitly constructs a <see cref="RealmValue"/> from <see cref="RealmInteger{T}">RealmInteger&lt;long&gt;</see>.
1418
        /// </summary>
1419
        /// <param name="val">The value to store in the <see cref="RealmValue"/>.</param>
1420
        /// <returns>A <see cref="RealmValue"/> containing the supplied <paramref name="val"/>.</returns>
1421
        public static implicit operator RealmValue(RealmInteger<long> val) => Int(val);
101✔
1422

1423
        /// <summary>
1424
        /// Implicitly constructs a <see cref="RealmValue"/> from <see cref="RealmInteger{T}">RealmInteger&lt;byte&gt;?</see>.
1425
        /// </summary>
1426
        /// <param name="val">The value to store in the <see cref="RealmValue"/>.</param>
1427
        /// <returns>A <see cref="RealmValue"/> containing the supplied <paramref name="val"/>.</returns>
1428
        public static implicit operator RealmValue(RealmInteger<byte>? val) => val == null ? Null : Int(val.Value);
74!
1429

1430
        /// <summary>
1431
        /// Implicitly constructs a <see cref="RealmValue"/> from <see cref="RealmInteger{T}">RealmInteger&lt;short&gt;?</see>.
1432
        /// </summary>
1433
        /// <param name="val">The value to store in the <see cref="RealmValue"/>.</param>
1434
        /// <returns>A <see cref="RealmValue"/> containing the supplied <paramref name="val"/>.</returns>
1435
        public static implicit operator RealmValue(RealmInteger<short>? val) => val == null ? Null : Int(val.Value);
72!
1436

1437
        /// <summary>
1438
        /// Implicitly constructs a <see cref="RealmValue"/> from <see cref="RealmInteger{T}">RealmInteger&lt;int&gt;?</see>.
1439
        /// </summary>
1440
        /// <param name="val">The value to store in the <see cref="RealmValue"/>.</param>
1441
        /// <returns>A <see cref="RealmValue"/> containing the supplied <paramref name="val"/>.</returns>
1442
        public static implicit operator RealmValue(RealmInteger<int>? val) => val == null ? Null : Int(val.Value);
74!
1443

1444
        /// <summary>
1445
        /// Implicitly constructs a <see cref="RealmValue"/> from <see cref="RealmInteger{T}">RealmInteger&lt;long&gt;?</see>.
1446
        /// </summary>
1447
        /// <param name="val">The value to store in the <see cref="RealmValue"/>.</param>
1448
        /// <returns>A <see cref="RealmValue"/> containing the supplied <paramref name="val"/>.</returns>
1449
        public static implicit operator RealmValue(RealmInteger<long>? val) => val == null ? Null : Int(val.Value);
74!
1450

1451
        /// <summary>
1452
        /// Implicitly constructs a <see cref="RealmValue"/> from <see cref="byte">byte[]?</see>.
1453
        /// </summary>
1454
        /// <param name="val">The value to store in the <see cref="RealmValue"/>.</param>
1455
        /// <returns>A <see cref="RealmValue"/> containing the supplied <paramref name="val"/>.</returns>
1456
        public static implicit operator RealmValue(byte[]? val) => val == null ? Null : Data(val);
4,467✔
1457

1458
        /// <summary>
1459
        /// Implicitly constructs a <see cref="RealmValue"/> from <see cref="string">string?</see>.
1460
        /// </summary>
1461
        /// <param name="val">The value to store in the <see cref="RealmValue"/>.</param>
1462
        /// <returns>A <see cref="RealmValue"/> containing the supplied <paramref name="val"/>.</returns>
1463
        public static implicit operator RealmValue(string? val) => val == null ? Null : String(val);
26,169✔
1464

1465
        /// <summary>
1466
        /// Implicitly constructs a <see cref="RealmValue"/> from <see cref="RealmObjectBase">RealmObjectBase?</see>.
1467
        /// </summary>
1468
        /// <param name="val">The value to store in the <see cref="RealmValue"/>.</param>
1469
        /// <returns>A <see cref="RealmValue"/> containing the supplied <paramref name="val"/>.</returns>
1470
        public static implicit operator RealmValue(RealmObjectBase? val) => val == null ? Null : Object(val);
1!
1471

1472
        /// <summary>
1473
        /// Implicitly constructs a <see cref="RealmValue"/> from <see cref="System.Collections.Generic.List{T}">List&lt;RealmValue&gt;?</see>.
1474
        /// </summary>
1475
        /// <param name="val">The value to store in the <see cref="RealmValue"/>.</param>
1476
        /// <returns>A <see cref="RealmValue"/> containing the supplied <paramref name="val"/>.</returns>
1477
        public static implicit operator RealmValue(List<RealmValue>? val) => val == null ? Null : List(val);
92!
1478

1479
        /// <summary>
1480
        /// Implicitly constructs a <see cref="RealmValue"/> from <see cref="RealmValue">RealmValue[]?</see>.
1481
        /// </summary>
1482
        /// <param name="val">The value to store in the <see cref="RealmValue"/>.</param>
1483
        /// <returns>A <see cref="RealmValue"/> containing the supplied <paramref name="val"/>.</returns>
1484
        public static implicit operator RealmValue(RealmValue[]? val) => val == null ? Null : List(val);
2!
1485

1486
        /// <summary>
1487
        /// Implicitly constructs a <see cref="RealmValue"/> from <see cref="System.Collections.Generic.Dictionary{TKey, TValue}">Dictionary&lt;string, RealmValue&gt;</see>.
1488
        /// </summary>
1489
        /// <param name="val">The value to store in the <see cref="RealmValue"/>.</param>
1490
        /// <returns>A <see cref="RealmValue"/> containing the supplied <paramref name="val"/>.</returns>
1491
        public static implicit operator RealmValue(Dictionary<string, RealmValue>? val) => val == null ? Null : Dictionary(val);
87!
1492

1493
        private void EnsureType(string target, RealmValueType type)
1494
        {
1495
            if (Type != type)
81,582✔
1496
            {
1497
                throw new InvalidCastException($"Can't cast to {target} since the underlying value is {Type}");
5✔
1498
            }
1499
        }
81,577✔
1500

1501
        internal readonly struct HandlesToCleanup
1502
        {
1503
            private readonly GCHandle _handle;
1504

1505
            // This is only needed for GeoPolygon. We could make it into an array, but
1506
            // that would mean we'd be allocating arrays even for a single handle argument
1507
            // and this happens on a hot path (string/byte[] property access). While this
1508
            // is quite ugly, it is cheaper and keeps all allocations on the stack, reducing
1509
            // GC pressure.
1510
            private readonly GCHandle? _handle2;
1511
            private readonly byte[]? _buffer;
1512

1513
            public HandlesToCleanup(GCHandle handle, byte[]? buffer = null, GCHandle? handle2 = null)
1514
            {
1515
                _handle = handle;
30,309✔
1516
                _buffer = buffer;
30,309✔
1517
                _handle2 = handle2;
30,309✔
1518
            }
30,309✔
1519

1520
            public void Dispose()
1521
            {
1522
                _handle.Free();
30,308✔
1523
                _handle2?.Free();
30,308✔
1524

1525
                if (_buffer != null)
30,308✔
1526
                {
1527
                    ArrayPool<byte>.Shared.Return(_buffer);
25,698✔
1528
                }
1529
            }
30,308✔
1530
        }
1531

1532
        /// <summary>
1533
        /// Indicates whether the current object is equal to another object of the same type.
1534
        /// </summary>
1535
        /// <param name="other">An object to compare with this object.</param>
1536
        /// <returns>true if the current object is equal to the other parameter; otherwise, false.</returns>
1537
        public bool Equals(RealmValue other)
1538
        {
1539
            if (other.Type != Type)
11,923✔
1540
            {
1541
                return false;
3,338✔
1542
            }
1543

1544
            return Type switch
8,585!
1545
            {
8,585✔
1546
                RealmValueType.Int => AsInt64() == other.AsInt64(),
1,653✔
1547
                RealmValueType.Bool => AsBool() == other.AsBool(),
405✔
1548
                RealmValueType.String => AsString() == other.AsString(),
823✔
1549
                RealmValueType.Data => AsData().SequenceEqual(other.AsData()),
234✔
1550
                RealmValueType.Date => AsDate() == other.AsDate(),
251✔
1551
                RealmValueType.Float => AsFloat() == other.AsFloat(),
171✔
1552
                RealmValueType.Double => AsDouble() == other.AsDouble(),
230✔
1553
                RealmValueType.Decimal128 => AsDecimal128() == other.AsDecimal128(),
231✔
1554
                RealmValueType.ObjectId => AsObjectId() == other.AsObjectId(),
3,529✔
1555
                RealmValueType.Guid => AsGuid() == other.AsGuid(),
407✔
1556
                RealmValueType.Object => AsIRealmObject().Equals(other.AsIRealmObject()),
162✔
1557
                RealmValueType.List => AsList().Equals(other.AsList()),
12✔
1558
                RealmValueType.Dictionary => AsDictionary().Equals(other.AsDictionary()),
12✔
1559
                RealmValueType.Null => true,
465✔
1560
                _ => false,
×
1561
            };
8,585✔
1562
        }
1563

1564
        /// <summary>
1565
        /// Compares two <see cref="RealmValue"/> instances for equality.
1566
        /// </summary>
1567
        /// <param name="left">The left instance.</param>
1568
        /// <param name="right">The right instance.</param>
1569
        /// <returns>
1570
        /// <c>true</c> if the underlying values stored in both instances are equal; <c>false</c> otherwise.
1571
        /// </returns>
1572
        public static bool operator ==(RealmValue left, RealmValue right)
1573
        {
1574
            return left.Equals(right);
1,607✔
1575
        }
1576

1577
        /// <summary>
1578
        /// Compares two <see cref="RealmValue"/> instances for inequality.
1579
        /// </summary>
1580
        /// <param name="left">The left instance.</param>
1581
        /// <param name="right">The right instance.</param>
1582
        /// <returns>
1583
        /// <c>true</c> if the underlying values stored in both instances are not equal; <c>false</c> otherwise.
1584
        /// </returns>
1585
        public static bool operator !=(RealmValue left, RealmValue right)
1586
        {
1587
            return !(left == right);
288✔
1588
        }
1589
    }
1590
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc