diff --git a/nanoFramework.CoreLibrary/System/Guid.cs b/nanoFramework.CoreLibrary/System/Guid.cs index 75e2f06..b2d9eff 100644 --- a/nanoFramework.CoreLibrary/System/Guid.cs +++ b/nanoFramework.CoreLibrary/System/Guid.cs @@ -18,6 +18,11 @@ public struct Guid /// public static readonly Guid Empty = new Guid(new byte[16]); + public Guid() + { + _data = new int[4]; // All zeros + } + /// /// Initializes a new instance of the structure by using the specified integers and bytes. /// @@ -103,7 +108,7 @@ public Guid(byte[] b) #pragma warning disable S3928 // Parameter names used into ArgumentException constructors should match an existing one if (b.Length != 16) { - throw new ArgumentException(); + throw new ArgumentException("Byte array must be 16 bytes long.", nameof(b)); } #pragma warning restore S3928 // Parameter names used into ArgumentException constructors should match an existing one @@ -160,29 +165,28 @@ public Guid(string g) /// is not a . public int CompareTo(object value) { - if (value == null) - { - return 1; - } - -#pragma warning disable S3928 // Parameter names used into ArgumentException constructors should match an existing one - if (value is not Guid) - { - throw new ArgumentException(); - } -#pragma warning restore S3928 // Parameter names used into ArgumentException constructors should match an existing one + if (value == null) return 1; + if (value is not Guid other) throw new ArgumentException("Object must be of type Guid."); - int[] other = ((Guid)value)._data; - other ??= new int[4]; + return CompareTo(other); + } + /// + /// Compares the current instance with another object of the same type and returns an integer that indicates whether the current instance precedes, follows, or occurs in the same position in the sort order as the other object. + /// + /// An object to compare with this instance. + /// A value that indicates the relative order of the objects being compared. + public int CompareTo(Guid other) + { + _data ??= new int[4]; + other._data ??= new int[4]; for (int i = 0; i < 4; i++) { - if (_data[i] != other[i]) + if (_data[i] != other._data[i]) { - return _data[i] - other[i]; + return _data[i] - other._data[i]; } } - return 0; } @@ -201,6 +205,8 @@ public int CompareTo(object value) /// public byte[] ToByteArray() { + _data ??= new int[4]; // Initialize if null (treat as Empty) + byte[] buffer = new byte[16]; int index = 0; @@ -289,10 +295,19 @@ public override bool Equals(object o) return false; } - int[] other = ((Guid)o)._data; - other ??= new int[4]; + return o is Guid other && Equals(other); + } - return (_data[0] == other[0]) && (_data[1] == other[1]) && (_data[2] == other[2]) && (_data[3] == other[3]); + /// + /// Indicates whether the current object is equal to another object of the same type. + /// + /// An object to compare with this object. + /// true if the current object is equal to the other parameter; otherwise, false. + public bool Equals(Guid other) + { + _data ??= new int[4]; + other._data ??= new int[4]; + return (_data[0] == other._data[0]) && (_data[1] == other._data[1]) && (_data[2] == other._data[2]) && (_data[3] == other._data[3]); } /// @@ -301,6 +316,7 @@ public override bool Equals(object o) /// The hash code for this instance. public override int GetHashCode() { + _data ??= new int[4]; // Initialize if null (treat as Empty) return _data[0] ^ _data[1] ^ _data[2] ^ _data[3]; } @@ -434,6 +450,8 @@ public static bool TryParseGuidWithDashes( return true; } + + /// /// Converts a hex sub-string to a long, while incrementing the parsePos. /// @@ -447,5 +465,34 @@ private static long HexStringToLong(string str, ref int parsePos, int requiredLe parsePos += requiredLength; return result; } + + /// + /// Determines whether two specified instances of are equal. + /// + /// The first to compare. + /// The second to compare. + /// if equals ; otherwise, . + public static bool operator ==(Guid a, Guid b) + { + // Defensive null handling, though _data should always be initialized + a._data ??= new int[4]; + b._data ??= new int[4]; + + return (a._data[0] == b._data[0]) && + (a._data[1] == b._data[1]) && + (a._data[2] == b._data[2]) && + (a._data[3] == b._data[3]); + } + + /// + /// Determines whether two specified instances of are not equal. + /// + /// The first to compare. + /// The second to compare. + /// if does not equal ; otherwise, . + public static bool operator !=(Guid a, Guid b) + { + return !(a == b); + } } }