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