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