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

Chapter 8 Language overview

1that unsafe code cannot masquerade as safe code. These restrictions limit the use of unsafe code to situations

2in which the code is trusted.

3The example

4using System;

5class Test

6{

7

static void WriteLocations(byte[] arr) {

8

unsafe {

9

fixed (byte* pArray = arr) {

10

byte* pElem = pArray;

11

for (int i = 0; i < arr.Length; i++) {

12

byte value = *pElem;

13

Console.WriteLine("arr[{0}] at 0x{1:X} is {2}",

14

i, (uint)pElem, value);

15

pElem++;

16

}

17

}

18

}

19

}

20

static void Main() {

21

byte[] arr = new byte[] {1, 2, 3, 4, 5};

22

WriteLocations(arr);

23}

24}

25shows an unsafe block in a method named WriteLocations that fixes an array instance and uses pointer

26manipulation to iterate over the elements. The index, value, and location of each array element are written to

27the console. One possible example of output is:

28arr[0] at 0x8E0360 is 1

29arr[1] at 0x8E0361 is 2

30arr[2] at 0x8E0362 is 3

31arr[3] at 0x8E0363 is 4

32arr[4] at 0x8E0364 is 5

33but, of course, the exact memory locations can be different in different executions of the application.

348.5 Expressions

35C# includes unary operators, binary operators, and one ternary operator. The following table summarizes the

36operators, listing them in order of precedence from highest to lowest:

37

27

C# LANGUAGE SPECIFICATION

Subclause

Category

Operators

 

 

 

 

 

 

 

14.5

Primary

x.y f(x) a[x] x++ x-- new

 

 

typeof

checked

unchecked

 

 

 

 

 

 

 

 

14.5.13

Unary

+

-

!

~ ++x

--x

(T)x

 

 

 

 

 

 

 

 

 

14.7

Multiplicative

*

/

%

 

 

 

 

 

 

 

 

 

 

 

 

 

14.7

Additive

+

-

 

 

 

 

 

 

 

 

 

 

 

 

 

 

14.8

Shift

<<

>>

 

 

 

 

 

 

 

 

 

 

 

 

 

 

14.9

Relational and

<

>

<=

>=

is

as

 

 

type-testing

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

14.9

Equality

==

!=

 

 

 

 

 

 

 

 

 

 

 

 

 

 

14.10

Logical AND

&

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

14.10

Logical XOR

^

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

14.10

Logical OR

|

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

14.11

Conditional

&&

 

 

 

 

 

 

 

AND

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

14.11

Conditional OR

||

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

14.12

Conditional

?:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

14.13

Assignment

=

*=

/=

%=

+=

-=

<<= >>= &= ^= |=

 

 

 

 

 

 

 

 

 

1

2 When an expression contains multiple operators, the precedence of the operators controls the order in which 3 the individual operators are evaluated. For example, the expression x + y * z is evaluated as

4 x + (y * z) because the * operator has higher precedence than the + operator.

5When an operand occurs between two operators with the same precedence, the associativity of the operators

6controls the order in which the operations are performed:

7Except for the assignment operators, all binary operators are left-associative, meaning that operations

8

are performed from left to right. For example, x + y + z is evaluated as (x + y) + z.

9

The assignment operators and the conditional operator (?:) are right-associative, meaning that

10

operations are performed from right to left. For example, x = y = z is evaluated as x = (y = z).

11

Precedence and associativity can be controlled using parentheses. For example, x + y * z first multiplies

12

y by z and then adds the result to x, but (x + y) * z first adds x and y and then multiplies the result by z.

138.6 Statements

14C# borrows most of its statements directly from C and C++, though there are some noteworthy additions and

15modifications. The table below lists the kinds of statements that can be used, and provides an example for

16each.

17

28

Chapter 8 Language overview

Statement

Example

 

 

Statement lists and block

static void Main() {

statements

F();

G();

 

 

{

 

H();

 

I();

 

}

 

}

 

 

Labeled statements and goto

static void Main(string[] args) {

statements

if (args.Length == 0)

goto done;

 

 

Console.WriteLine(args.Length);

 

done:

 

Console.WriteLine("Done");

 

}

 

 

Local constant declarations

static void Main() {

 

const float pi = 3.14f;

 

const int r = 123;

 

Console.WriteLine(pi * r * r);

 

}

 

 

Local variable declarations

static void Main() {

 

int a;

 

int b = 2, c = 3;

 

a = 1;

 

Console.WriteLine(a + b + c);

 

}

 

 

Expression statements

static int F(int a, int b) {

 

return a + b;

 

}

 

static void Main() {

 

F(1, 2); // Expression statement

 

}

 

 

if statements

static void Main(string[] args) {

 

if (args.Length == 0)

 

Console.WriteLine("No args");

 

else

 

Console.WriteLine("Args");

 

}

 

 

switch statements

static void Main(string[] args) {

 

switch (args.Length) {

 

case 0:

 

Console.WriteLine("No args");

 

break;

 

case 1:

 

Console.WriteLine("One arg ");

 

break;

 

default:

 

int n = args.Length;

 

Console.WriteLine("{0} args", n);

 

break;

 

}

 

}

 

 

while statements

static void Main(string[] args) {

 

int i = 0;

 

while (i < args.Length) {

 

Console.WriteLine(args[i]);

 

i++;

 

}

 

}

 

 

29

C# LANGUAGE SPECIFICATION

do statements

static void Main() {

 

string s;

 

do { s = Console.ReadLine(); }

 

while (s != "Exit");

 

}

 

 

for statements

static void Main(string[] args) {

 

for (int i = 0; i < args.Length; i++)

 

Console.WriteLine(args[i]);

 

}

 

 

foreach statements

static void Main(string[] args) {

 

foreach (string s in args)

 

Console.WriteLine(s);

 

}

 

 

break statements

static void Main(string[] args) {

 

int i = 0;

 

while (true) {

 

if (i == args.Length)

 

break;

 

Console.WriteLine(args[i++]);

 

}

 

}

 

 

continue statements

static void Main(string[] args) {

 

int i = 0;

 

while (true) {

 

Console.WriteLine(args[i++]);

 

if (i < args.Length)

 

continue;

 

break;

 

}

 

}

 

 

return statements

static int F(int a, int b) {

 

return a + b;

 

}

 

static void Main() {

 

Console.WriteLine(F(1, 2));

 

return;

 

}

 

 

yield statements

static IEnumerable<int> FromTo(int a, int b) {

 

if (a > b)

 

yield break;

 

for ( ; ; a++) {

 

yield return a;

 

if (a == b)

 

break;

 

}

 

}

 

 

throw statements and try

static int F(int a, int b) {

statements

if (b == 0)

throw new Exception("Divide by zero");

 

 

return a / b;

 

}

 

static void Main() {

 

try {

 

Console.WriteLine(F(5, 0));

 

}

 

catch(Exception e) {

 

Console.WriteLine("Error");

 

}

 

}

 

 

30

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