2 <clause number="18.4.1" title="Database integer type" informative="true">
3 <paragraph>The DBInt struct below implements an integer type that can represent the complete set of values of the <keyword>int</keyword> type, plus an additional state that indicates an unknown value. A type with these characteristics is commonly used in databases. <code_example><![CDATA[
7 // The Null member represents an unknown DBInt value.
8 public static readonly DBInt Null = new DBInt();
9 // When the defined field is true, this DBInt represents a known value
10 // which is stored in the value field. When the defined field is false,
12 // this DBInt represents an unknown value, and the value field is 0.
15 // Private instance constructor. Creates a DBInt with a known value.
20 // The IsNull property is true if this DBInt represents an unknown value.
22 public bool IsNull { get { return !defined; } }
23 // The Value property is the known value of this DBInt, or 0 if this
24 // DBInt represents an unknown value.
25 public int Value { get { return value; } }
26 // Implicit conversion from int to DBInt.
27 public static implicit operator DBInt(int x) {
30 // Explicit conversion from DBInt to int. Throws an exception if the
31 // given DBInt represents an unknown value.
32 public static explicit operator int(DBInt x) {
33 if (!x.defined) throw new InvalidOperationException();
36 public static DBInt operator +(DBInt x) {
39 public static DBInt operator -(DBInt x) {
40 return x.defined? -x.value: Null;
42 public static DBInt operator +(DBInt x, DBInt y) {
43 return x.defined && y.defined? x.value + y.value: Null;
45 public static DBInt operator -(DBInt x, DBInt y) {
46 return x.defined && y.defined? x.value - y.value: Null;
48 public static DBInt operator *(DBInt x, DBInt y) {
49 return x.defined && y.defined? x.value * y.value: Null;
51 public static DBInt operator /(DBInt x, DBInt y) {
52 return x.defined && y.defined? x.value / y.value: Null;
54 public static DBInt operator %(DBInt x, DBInt y) {
55 return x.defined && y.defined? x.value % y.value: Null;
57 public static DBBool operator ==(DBInt x, DBInt y) {
58 return x.defined && y.defined? x.value == y.value: DBBool.Null;
60 public static DBBool operator !=(DBInt x, DBInt y) {
61 return x.defined && y.defined? x.value != y.value: DBBool.Null;
63 public static DBBool operator >(DBInt x, DBInt y) {
64 return x.defined && y.defined? x.value > y.value: DBBool.Null;
66 public static DBBool operator <(DBInt x, DBInt y) {
67 return x.defined && y.defined? x.value < y.value: DBBool.Null;
69 public static DBBool operator >=(DBInt x, DBInt y) {
70 return x.defined && y.defined? x.value >= y.value: DBBool.Null;
72 public static DBBool operator <=(DBInt x, DBInt y) {
73 return x.defined && y.defined? x.value <= y.value: DBBool.Null;
76 ]]></code_example></paragraph>