www.pudn.com > sudoku.rar > NullableByte.cs
//-------------------------------------------------------------------------- // // Copyright (c) Microsoft Corporation. All rights reserved. // // File: NullableByte.cs // // Description: .NET 1.x substitute for Nullablein 2.0 // //-------------------------------------------------------------------------- using System; using System.Text; namespace Microsoft.Sudoku.Nullables { /// A nullable byte. [Serializable] public struct NullableByte { ///The contained value. private byte _value; ///Whether this nullable has a value. private bool _hasValue; ///Initializes the nullable. /// The value with which to initialize this nullable. public NullableByte(byte value) { _value = value; _hasValue = true; } ///Gets the value of the nullable. public byte Value { get { if (!_hasValue) throw new InvalidOperationException(); return _value; } } ///Gets whether this nullable has a value. public bool HasValue { get { return _hasValue; } } ///Implicit cast from a NullableByte to a byte. /// The value to be cast. ///The cast byte. public static implicit operator byte(NullableByte value) { return value.Value; } ///Implicit cast from a byte to a NullableByte. /// The value to be cast. ///The cast NullableByte. public static implicit operator NullableByte(byte value) { return new NullableByte(value); } ///Implicit cast from a null value to a NullableByte. /// Must be null. ///A null NullableByte. public static implicit operator NullableByte(NullObject value) { if (value != null) throw new ArgumentOutOfRangeException("value"); return new NullableByte(); } ///Equality check for two NullableBytes. /// The first value. /// The second value. ///true if the values are equal; otherwise, false. public static bool operator ==(NullableByte value1, NullableByte value2) { return (!value1.HasValue && !value2.HasValue) || (value1.HasValue && value2.HasValue && value1._value == value2._value); } ///Inequality check for two NullableBytes. /// The first value. /// The second value. ///true if the values are equal; otherwise, false. public static bool operator !=(NullableByte value1, NullableByte value2) { return !(value1 == value2); } ///Equality check for a NullableByte and a byte. /// The first value. /// The second value. ///true if the values are equal; otherwise, false. public static bool operator ==(NullableByte value1, byte value2) { return value1.HasValue && value1.Value == value2; } ///Inequality check for a NullableByte and a byte. /// The first value. /// The second value. ///true if the values are equal; otherwise, false. public static bool operator !=(NullableByte value1, byte value2) { return !(value1 == value2); } ///Equality check for a NullableByte and a null value. /// The first value. /// The null value. ///true if the values are equal; otherwise, false. public static bool operator ==(NullableByte value1, NullObject value2) { return !value1.HasValue && value2 == null; } ///Inequality check for a NullableByte and a null value. /// The first value. /// The null value. ///true if the values are equal; otherwise, false. public static bool operator !=(NullableByte value1, NullObject value2) { return !(value1 == value2); } ////// /// TheDetermines whether the specified ///is equal to the current . to compare with the current . /// /// public override bool Equals(object obj) { if (obj is NullableByte) { return this == (NullableByte)obj; } else if (obj is byte) return HasValue && _value == (byte)obj; else if (obj == null) return !HasValue; else return false; } ////// ///if the specified is equal to the /// current ; otherwise, . /// ///Serves as a hash function for a particular type, suitable /// for use in hashing algorithms and data structures like a hash table. ////// public override int GetHashCode() { return base.GetHashCode(); } } }A hash code for the current ///.