www.pudn.com > sudoku.rar > NullableUint.cs
//-------------------------------------------------------------------------- // // Copyright (c) Microsoft Corporation. All rights reserved. // // File: NullableUint.cs // // Description: .NET 1.x substitute for Nullablein 2.0 // //-------------------------------------------------------------------------- using System; using System.Text; namespace Microsoft.Sudoku.Nullables { /// A nullable uint. [Serializable] public struct NullableUint { ///The contained value. private uint _value; ///Whether this nullable has a value. private bool _hasValue; ///Initializes the nullable. /// The value with which to initialize this nullable. public NullableUint(uint value) { _value = value; _hasValue = true; } ///Gets the value of the nullable. public uint 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 NullableUint to a uint. /// The value to be cast. ///The cast uint. public static implicit operator uint(NullableUint value) { return value.Value; } ///Implicit cast from a uint to a NullableUint. /// The value to be cast. ///The cast NullableUint. public static implicit operator NullableUint(uint value) { return new NullableUint(value); } ///Implicit cast from a null value to a NullableUint. /// Must be null. ///A null NullableUint. public static implicit operator NullableUint(NullObject value) { if (value != null) throw new ArgumentOutOfRangeException("value"); return new NullableUint(); } ///Equality check for two NullableUints. /// The first value. /// The second value. ///true if the values are equal; otherwise, false. public static bool operator ==(NullableUint value1, NullableUint value2) { return (!value1.HasValue && !value2.HasValue) || (value1.HasValue && value2.HasValue && value1._value == value2._value); } ///Inequality check for two NullableUints. /// The first value. /// The second value. ///true if the values are equal; otherwise, false. public static bool operator !=(NullableUint value1, NullableUint value2) { return !(value1 == value2); } ///Equality check for a nullable and an uint. /// The first value. /// The second value. ///true if the values are equal; otherwise, false. public static bool operator ==(NullableUint value1, uint value2) { return value1.HasValue && value1.Value == value2; } ///Inequality check for a nullable and an uint. /// The first value. /// The second value. ///true if the values are equal; otherwise, false. public static bool operator !=(NullableUint value1, uint value2) { return !(value1 == value2); } ///Equality check for a NullableUint and a null value. /// The first value. /// The null value. ///true if the values are equal; otherwise, false. public static bool operator ==(NullableUint value1, NullObject value2) { return !value1.HasValue && value2 == null; } ///Inequality check for a NullableUint and a null value. /// The first value. /// The null value. ///true if the values are equal; otherwise, false. public static bool operator !=(NullableUint 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 NullableUint) return base.Equals(obj); else if (obj is uint) return HasValue && Value == (uint)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 ///.