Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C-sharp language specification.2004.pdf
Скачиваний:
14
Добавлен:
23.08.2013
Размер:
2.55 Mб
Скачать

C# LANGUAGE SPECIFICATION

1A static member of the struct is referenced.

2An explicitly declared constructor of the struct is called.

3[Note: The creation of default values (§18.3.4) of struct types does not trigger the static constructor. (An

4example of this is the initial value of elements in an array.) end note]

518.4 Struct examples

6This whole subclause is informative.

718.4.1 Database integer type

8The DBInt struct below implements an integer type that can represent the complete set of values of the int

9type, plus an additional state that indicates an unknown value. A type with these characteristics is commonly

10used in databases.

11using System;

12public struct DBInt

13{

14

// The Null member represents an unknown DBInt value.

15

public static readonly DBInt Null = new DBInt();

16

// When the defined field is true, this DBInt represents a

17

// known value which is stored in the value field. When

18

// the defined field is false, this DBInt represents an

19

// unknown value, and the value field is 0.

20

int value;

21

bool defined;

22

// Private instance constructor. Creates a DBInt with a known value.

23

DBInt(int value) {

24

this.value = value;

25

this.defined = true;

26

}

27

// The IsNull property is true if this DBInt represents an unknown

28

// value.

29

public bool IsNull { get { return !defined; } }

30

// The Value property is the known value of this DBInt, or 0 if this

31

// DBInt represents an unknown value.

32

public int Value { get { return value; } }

33

// Implicit conversion from int to DBInt.

34

public static implicit operator DBInt(int x) {

35

return new DBInt(x);

36

}

37

// Explicit conversion from DBInt to int. Throws an exception if the

38

// given DBInt represents an unknown value.

39

public static explicit operator int(DBInt x) {

40

if (!x.defined) throw new InvalidOperationException();

41

return x.value;

42

}

43

public static DBInt operator +(DBInt x) {

44

return x;

45

}

46

public static DBInt operator -(DBInt x) {

47

return x.defined? -x.value: Null;

48

}

49

public static DBInt operator +(DBInt x, DBInt y) {

50

return x.defined && y.defined? x.value + y.value: Null;

51

}

326

 

Chapter 18 Structs

1

public static DBInt operator -(DBInt x, DBInt y) {

2

return x.defined && y.defined? x.value - y.value: Null;

3

}

4

public static DBInt operator *(DBInt x, DBInt y) {

5

return x.defined && y.defined? x.value * y.value: Null;

6

}

7

public static DBInt operator /(DBInt x, DBInt y) {

8

return x.defined && y.defined? x.value / y.value: Null;

9

}

10

public static DBInt operator %(DBInt x, DBInt y) {

11

return x.defined && y.defined? x.value % y.value: Null;

12

}

13

public static DBBool operator ==(DBInt x, DBInt y) {

14

return x.defined && y.defined? x.value == y.value: DBBool.Null;

15

}

16

public static DBBool operator !=(DBInt x, DBInt y) {

17

return x.defined && y.defined? x.value != y.value: DBBool.Null;

18

}

19

public static DBBool operator >(DBInt x, DBInt y) {

20

return x.defined && y.defined? x.value > y.value: DBBool.Null;

21

}

22

public static DBBool operator <(DBInt x, DBInt y) {

23

return x.defined && y.defined? x.value < y.value: DBBool.Null;

24

}

25

public static DBBool operator >=(DBInt x, DBInt y) {

26

return x.defined && y.defined? x.value >= y.value: DBBool.Null;

27

}

28

public static DBBool operator <=(DBInt x, DBInt y) {

29

return x.defined && y.defined? x.value <= y.value: DBBool.Null;

30}

31}

3218.4.2 Database boolean type

33The DBBool struct below implements a three-valued logical type. The possible values of this type are

34DBBool.True, DBBool.False, and DBBool.Null, where the Null member indicates an unknown value.

35Such three-valued logical types are commonly used in databases.

36using System;

37public struct DBBool

38{

39

// The three possible DBBool values.

40

public static readonly DBBool Null = new DBBool(0);

41

public static readonly DBBool False = new DBBool(-1);

42

public static readonly DBBool True = new DBBool(1);

43

// Private field that stores –1, 0, 1 for False, Null, True.

44

sbyte value;

45

// Private instance constructor. The value parameter must be

46

// –1, 0, or 1.

47

DBBool(int value) {

48

this.value = (sbyte)value;

49

}

50

// Properties to examine the value of a DBBool. Return true if this

51

// DBBool has the given value, false otherwise.

52

public bool IsNull { get { return value == 0; } }

53

public bool IsFalse { get { return value < 0; } }

54

public bool IsTrue { get { return value > 0; } }

327

 

C# LANGUAGE SPECIFICATION

1

// Implicit conversion from bool to DBBool. Maps true to

2

// DBBool.True and false to DBBool.False.

3

public static implicit operator DBBool(bool x) {

4

return x? True: False;

5

}

6

// Explicit conversion from DBBool to bool. Throws an exception

7

// if the given DBBool is Null, otherwise returns true or false.

8

public static explicit operator bool(DBBool x) {

9

if (x.value == 0) throw new InvalidOperationException();

10

return x.value > 0;

11

}

12

// Equality operator. Returns Null if either operand is Null,

13

// otherwise returns True or False.

14

public static DBBool operator ==(DBBool x, DBBool y) {

15

if (x.value == 0 || y.value == 0) return Null;

16

return x.value == y.value? True: False;

17

}

18

// Inequality operator. Returns Null if either operand is Null,

19

// otherwise returns True or False.

20

public static DBBool operator !=(DBBool x, DBBool y) {

21

if (x.value == 0 || y.value == 0) return Null;

22

return x.value != y.value? True: False;

23

}

24

// Logical negation operator. Returns True if the operand is False,

25

// Null if the operand is Null, or False if the operand is True.

26

public static DBBool operator !(DBBool x) {

27

return new DBBool(-x.value);

28

}

29

// Logical AND operator. Returns False if either operand is False,

30

// otherwise Null if either operand is Null, otherwise True.

31

public static DBBool operator &(DBBool x, DBBool y) {

32

return new DBBool(x.value < y.value? x.value: y.value);

33

}

34

// Logical OR operator. Returns True if either operand is True,

35

// otherwise Null if either operand is Null, otherwise False.

36

public static DBBool operator |(DBBool x, DBBool y) {

37

return new DBBool(x.value > y.value? x.value: y.value);

38

}

39

// Definitely true operator. Returns true if the operand is True,

40

// false otherwise.

41

public static bool operator true(DBBool x) {

42

return x.value > 0;

43

}

44

// Definitely false operator. Returns true if the operand is False,

45

// false otherwise.

46

public static bool operator false(DBBool x) {

47

return x.value < 0;

48}

49}

50End of informative text.

328

Соседние файлы в предмете Электротехника