18.4 Struct examples

原创 2006年05月25日 05:41:00
This whole clause is informative.
C# LANGUAGE SPECIFICATION
272
18.4.1 Database integer type
The DBInt struct below implements an integer type that can represent the
complete set of values of the int
type, plus an additional state that indicates an unknown value. A type with
these characteristics is commonly
used in databases.
using System;
public struct DBInt
{
// The Null member represents an unknown DBInt value.
public static readonly DBInt Null = new DBInt();
// When the defined field is true, this DBInt represents a
// known value which is stored in the value field. When
// the defined field is false, this DBInt represents an
// unknown value, and the value field is 0.
int value;
bool defined;
// Private instance constructor. Creates a DBInt with a known value.
DBInt(int value) {
this.value = value;
this.defined = true;
}
// The IsNull property is true if this DBInt represents an unknown
// value.
public bool IsNull { get { return !defined; } }
// The Value property is the known value of this DBInt, or 0 if this
// DBInt represents an unknown value.
public int Value { get { return value; } }
// Implicit conversion from int to DBInt.
public static implicit operator DBInt(int x) {
return new DBInt(x);
}
// Explicit conversion from DBInt to int. Throws an exception if the
// given DBInt represents an unknown value.
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;
}
Chapter 18 Structs
273
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;
}
}
18.4.2 Database boolean type
The DBBool struct below implements a three-valued logical type. The
possible values of this type are
DBBool.True, DBBool.False, and DBBool.Null, where the Null member indicates
an unknown value.
Such three-valued logical types are commonly used in databases.
using System;
public struct DBBool
{
// The three possible DBBool values.
public static readonly DBBool Null = new DBBool(0);
public static readonly DBBool False = new DBBool(-1);
public static readonly DBBool True = new DBBool(1);
// Private field that stores ?1, 0, 1 for False, Null, True.
sbyte value;
// Private instance constructor. The value parameter must be
// ?1, 0, or 1.
DBBool(int value) {
this.value = (sbyte)value;
}
// Properties to examine the value of a DBBool. Return true if this
// DBBool has the given value, false otherwise.
public bool IsNull { get { return value == 0; } }
public bool IsFalse { get { return value < 0; } }
public bool IsTrue { get { return value > 0; } }
// Implicit conversion from bool to DBBool. Maps true to
// DBBool.True and false to DBBool.False.
public static implicit operator DBBool(bool x) {
return x? True: False;
}
// Explicit conversion from DBBool to bool. Throws an exception
// if the given DBBool is Null, otherwise returns true or false.
C# LANGUAGE SPECIFICATION
274
public static explicit operator bool(DBBool x) {
if (x.value == 0) throw new InvalidOperationException();
return x.value > 0;
}
// Equality operator. Returns Null if either operand is Null,
// otherwise returns True or 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;
}
// Inequality operator. Returns Null if either operand is Null,
// otherwise returns True or 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;
}
// Logical negation operator. Returns True if the operand is False,
// Null if the operand is Null, or False if the operand is True.
public static DBBool operator !(DBBool x) {
return new DBBool(-x.value);
}
// Logical AND operator. Returns False if either operand is False,
// otherwise Null if either operand is Null, otherwise True.
public static DBBool operator &(DBBool x, DBBool y) {
return new DBBool(x.value < y.value? x.value: y.value);
}
// Logical OR operator. Returns True if either operand is True,
// otherwise Null if either operand is Null, otherwise False.
public static DBBool operator |(DBBool x, DBBool y) {
return new DBBool(x.value > y.value? x.value: y.value);
}
// Definitely true operator. Returns true if the operand is True,
// false otherwise.
public static bool operator true(DBBool x) {
return x.value > 0;
}
// Definitely false operator. Returns true if the operand is False,
// false otherwise.
public static bool operator false(DBBool x) {
return x.value < 0;
}
}
End of informative text.

WebSocket实例—初级聊天室(来自Tomcat8的examples)

程序跑起来在Tomcat的Webapps中自带有Tomcat的很多小例子。本文选取其中的关于WebSocket的例子,来进行学习。首先先将程序跑起来。 开发环境eclipse+tomcat8 1、...
  • fumier
  • fumier
  • 2015年10月20日 16:08
  • 2057

源代码安装MySQL5.6

本文描述了编译安装MySQL5.6的全过程。
  • renfengjun
  • renfengjun
  • 2014年11月27日 22:26
  • 5519

Oracle 11g静默安装官方提供的examples

官网下载examples包并解压 准备响应文件 执行静默安装 执行sql脚本文件安装特定模式 在我的博文linux系统上静默安装ORACLE 11g中,详细说明了纯命令行安装Oracle11g的整个过...
  • smstong
  • smstong
  • 2015年05月21日 14:42
  • 3421

LeetCode 18.4Sum

题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d...
  • caishenfans
  • caishenfans
  • 2015年02月09日 23:33
  • 296

[leetcode: Python]18.4Sum

Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar...
  • NXHYD
  • NXHYD
  • 2017年05月19日 08:18
  • 236

《Drools6.4 中文文档》第18章18.4 workbench配置

18.4. 配置18.4.1. 基本用户管理Workbench的认证系统采用服务器的用户鉴别和授权。 Jboss 的EAP和WildFly,添加一个用户在脚本文件$JBOSS_HOME/bin/ad...
  • wo541075754
  • wo541075754
  • 2016年09月30日 10:38
  • 1790

从头认识java-18.4 java多线程原子性操作里面的坑

这一章节我们来讨论一下java多线程原子性里面的坑。1.看似原子性操作:package com.ray.ch18; public class Test { private int a = 0; ...
  • raylee2007
  • raylee2007
  • 2016年01月25日 22:34
  • 1385

[leetcode]18.4Sum(Java实现)

leetcode地址:https://leetcode.com/problems/4sum/#/description Given an array S of n integers,...
  • zjkC050818
  • zjkC050818
  • 2017年06月19日 09:37
  • 100

Performing User-Managed Database-18.4、Restoring Datafiles and Archived Redo Logs

18.4、Restoring Datafiles and Archived Redo Logs 如果介质失败损坏一个或多个数据文件,恢复损坏的文件前必须还原数据文件。如果还原的位置不是原始位置,必须在...
  • offbeatmine
  • offbeatmine
  • 2014年06月04日 16:48
  • 1100

leetCode 18.4Sum (4数字和) 解题思路和方法

4Sum  Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + ...
  • xygy8860
  • xygy8860
  • 2015年07月06日 16:00
  • 403
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:18.4 Struct examples
举报原因:
原因补充:

(最多只允许输入30个字)