11.4 结构示例
11.4 结构示例
下面各节将展示两个关于使用struct类型的重要示例,它们将各自创建一个类型,这两个类型可以像C#语言的内置类型那样使用,但它们分别具有修改了的语义。
11.4.1 数据库整型
下面的DBInt结构将实现一个整型,这个整型可以表示int类型的值的完整集合,再加上一个用于表示未知值的附加状态。具有这些特征的类型常用在数据库中。
using System;
public struct DBInt
{
//Null成员表示未知的DBInt值。
public static readonly DBInt Null = new DBInt();
//如果defined域为true,则DBInt表示一个已知的值,该值存储在value域中。
//如果defined域为false,则DBInt表示一个未知的值,并且value域的值为0。
int value;
bool defined;
//私有实例构造函数,使用一个已知的值创建DBInt。
DBInt(int value) {
this.value = value;
this.defined = true;
}
//如果DBInt表示一个未知的值,则IsNull属性为true。
public bool IsNull { get { return !defined; } }
//Value属性是DBInt的已知值,如果DBInt表示一个未知的值,则该属性为0。
public int Value { get { return value; } }
//从int到DBInt的隐式转换。
public static implicit operator DBInt(int x) {
return new DBInt(x);
}
//从DBInt到int的显式转换。如果给定的DBInt表示一个未知的值,则引发异常。
public static explicit operator int(DBInt x) {
if (!x.defined) throw new InvalidOperationException();
return x.value;
}
public static DBInt operator +(DBInt x) {
return x;
}
public static DBInt operator -(DBInt x) {
return x.defined ? -x.value : Null;
}
public static DBInt operator +(DBInt x, DBInt y) {
return x.defined && y.defined? x.value + y.value: Null;
}
public static DBInt operator -(DBInt x, DBInt y) {
return x.defined && y.defined? x.value - y.value: Null;
}
public static DBInt operator *(DBInt x, DBInt y) {
return x.defined && y.defined? x.value * y.value: Null;
}
public static DBInt operator /(DBInt x, DBInt y) {
return x.defined && y.defined? x.value / y.value: Null;
}
public static DBInt operator %(DBInt x, DBInt y) {
return x.defined && y.defined? x.value % y.value: Null;
}
public static DBBool operator ==(DBInt x, DBInt y) {
return x.defined && y.defined? x.value == y.value: DBBool.Null;
}
public static DBBool operator !=(DBInt x, DBInt y) {
return x.defined && y.defined? x.value != y.value: DBBool.Null;
}
public static DBBool operator >(DBInt x, DBInt y) {
return x.defined && y.defined? x.value > y.value: DBBool.Null;
}
public static DBBool operator <(DBInt x, DBInt y) {
return x.defined && y.defined? x.value < y.value: DBBool.Null;
}
public static DBBool operator >=(DBInt x, DBInt y) {
return x.defined && y.defined? x.value >= y.value: DBBool.Null;
}
public static DBBool operator <=(DBInt x, DBInt y) {
return x.defined && y.defined? x.value <= y.value: DBBool.Null;
}
public override bool Equals(object obj) {
if (!(obj is DBInt)) return false;
DBInt x = (DBInt)obj;
return value == x.value && defined == x.defined;
}
public override int GetHashCode() {
return value;
}
public override string ToString() {
return defined? value.ToString():"DBInt.Null";
}
}
11.4.2 数据库布尔类型
下面的DBBool结构将实现一个三值逻辑类型。该类型的可能值为DBBool.True、DBBool.False和DBBool.Null,其中Null成员用于表示未知值。这样的三值逻辑类型常用在数据库中。
using System;
public struct DBBool
{
//3个可能的DBBool值。
public static readonly DBBool Null = new DBBool(0);
public static readonly DBBool False = new DBBool(-1);
public static readonly DBBool True = new DBBool(1);
//私有域,使用-1、0、1分别表示False、Null、True。
sbyte value;
//私有实例构造函数,值参数必须为-1、0或1。
DBBool(int value) {
this.value = (sbyte)value;
}
//检查DBBool的值的属性。如果DBBool具有给定的值,则返回true;否则返回false。
public bool IsNull { get { return value == 0; } }
public bool IsFalse { get { return value < 0; } }
public bool IsTrue { get { return value > 0; } }
//从bool到DBBool的隐式转换。
//将true映射为DBBool.True,将false映射为DBBool.False。
public static implicit operator DBBool(bool x) {
return x? True: False;
}
//从DBBool到bool的显式转换。
//如果给定DBBool为Null,则引发一个异常。否则,返回true或false。
public static explicit operator bool(DBBool x) {
if (x.value == 0) throw new InvalidOperationException();
return x.value > 0;
}
//相等运算符。如果任何一个操作数为Null,则返回Null;否则,返回true或false。
public static DBBool operator ==(DBBool x, DBBool y) {
if (x.value == 0 || y.value == 0) return Null;
return x.value == y.value? True: False;
}
//不相等运算符。如果任何一个操作数为Null,则返回Null;否则,返回true或false。
public static DBBool operator !=(DBBool x, DBBool y) {
if (x.value == 0 || y.value == 0) return Null;
return x.value != y.value? True: False;
}
//逻辑非运算符。如果操作数为false,则返回true;如果操作数为true,则返回false;
//如果操作数为Null,则返回Null。
public static DBBool operator !(DBBool x) {
return new DBBool(-x.value);
}
//逻辑与运算符。如果任何一个操作数为false,则返回false;
//否则,如果任何一个操作数为Null,则返回Null;其他情况返回true。
public static DBBool operator &(DBBool x, DBBool y) {
return new DBBool(x.value < y.value? x.value: y.value);
}
//逻辑或运算符。如果任何一个操作数为true,则返回true;
//否则,如果任何一个操作数为Null,则返回Null;其他情况返回false。
public static DBBool operator |(DBBool x, DBBool y) {
return new DBBool(x.value > y.value? x.value: y.value);
}
//true运算符。如果操作数为true,则返回true;否则,返回false。
public static bool operator true(DBBool x) {
return x.value > 0;
}
//false运算符。如果操作数为false,则返回true;否则,返回false。
public static bool operator false(DBBool x) {
return x.value < 0;
}
public override bool Equals(object obj) {
if (!(obj is DBBool)) return false;
return value == ((DBBool)obj).value;
}
public override int GetHashCode() {
return value;
}
public override string ToString() {
if (value > 0) return "DBBool.True";
if (value < 0) return "DBBool.False";
return "DBBool.Null";
}
}