[ Pobierz całość w formacie PDF ]

A local constant declaration declares one or more local constants, and a local variable declaration declares one
or more local variables.
The example
class Test
{
static void Main() {
const int a = 1;
const int b = 2, c = 3;
int d;
int e, f;
int g = 4, h = 5;
d = 4;
e = 5;
f = 6;
}
}
shows a variety of local constant and variable declarations.
1.7.4 Expression statements
An expression statement evaluates a given expression. The value computed by the expression, if any, is
discarded. Not all expressions are permitted as statements. In particular, expressions such as x + y and x == 1
that have no side effects, but merely compute a value (which will be discarded), are not permitted as statements.
The example
using System;
class Test
{
static int F() {
Console.WriteLine("Test.F");
return 0;
}
static void Main() {
F();
}
}
shows an expression statement. The call to the function F made from Main constitutes an expression statement.
The value that F returns is simply discarded.
1.7.5 The if statement
An if statement selects a statement for execution based on the value of a boolean expression. An if statement
may optionally include an else clause that executes if the boolean expression is false.
The example
using System;
Copyright Microsoft Corporation 1999-2000. All Rights Reserved. 11
C# LANGUAGE REFERENCE
class Test
{
static void Main(string[] args) {
if (args.Length == 0)
Console.WriteLine("No arguments were provided");
else
Console.WriteLine("Arguments were provided");
}
}
shows a program that uses an if statement to write out two different messages depending on whether command-
line arguments were provided or not.
1.7.6 The switch statement
A switch statement executes the statements that are associated with the value of a given expression, or a
default of statements if no match exists.
The example
using System;
class Test
{
static void Main(string[] args) {
switch (args.Length) {
case 0:
Console.WriteLine("No arguments were provided");
break;
case 1:
Console.WriteLine("One arguments was provided");
break;
default:
Console.WriteLine("{0} arguments were provided");
break;
}
}
}
switches on the number of arguments provided.
1.7.7 The while statement
A while statement conditionally executes a statement zero or more times  as long as a boolean test is true.
using System;
class Test
{
static int Find(int value, int[] arr) {
int i = 0;
while (arr[i] != value) {
if (++i > arr.Length)
throw new ArgumentException();
}
return i;
}
static void Main() {
Console.WriteLine(Find(3, new int[] {5, 4, 3, 2, 1}));
}
}
uses a while statement to find the first occurrence of a value in an array.
12 Copyright Microsoft Corporation 1999-2000. All Rights Reserved.
Chapter 1 Introduction
1.7.8 The do statement
A do statement conditionally executes a statement one or more times.
The example
using System;
class Test
{
static void Main() {
string s;
do {
s = Console.ReadLine();
}
while (s != "Exit");
}
}
reads from the console until the user types  Exit and presses the enter key.
1.7.9 The for statement
A for statement evaluates a sequence of initialization expressions and then, while a condition is true, repeatedly
executes a statement and evaluates a sequence of iteration expressions.
The example
using System;
class Test
{
static void Main() {
for (int i = 0; i
Console.WriteLine(i);
}
}
uses a for statement to write out the integer values 1 through 10.
1.7.10 The foreach statement
A foreach statement enumerates the elements of a collection, executing a statement for each element of the
collection.
The example
using System;
using System.Collections;
class Test
{
static void WriteList(ArrayList list) {
foreach (object o in list)
Console.WriteLine(o);
}
static void Main() {
ArrayList list = new ArrayList();
for (int i = 0; i
list.Add(i);
WriteList(list);
}
}
Copyright Microsoft Corporation 1999-2000. All Rights Reserved. 13
C# LANGUAGE REFERENCE
uses a foreach statement to iterate over the elements of a list.
1.7.11 The break statement and the continue statement
A break statement exits the nearest enclosing switch, while, do, for, or foreach statement; a continue
starts a new iteration of the nearest enclosing while, do, for, or foreach statement.
1.7.12 The return statement
A return statement returns control to the caller of the member in which the return statement appears. A
return statement with no expression can be used only in a member that does not return a value (e.g., a method
that returns void). A return statement with an expression can only be used only in a function member that
returns an expression.
1.7.13 The throw statement
The throw statement throws an exception.
1.7.14 The try statement
The try statement provides a mechanism for catching exceptions that occur during execution of a block. The
try statement furthermore provides the ability to specify a block of code that is always executed when control
leaves the try statement.
1.7.15 The checked and unchecked statements
The checked and unchecked statements are used to control the overflow checking context for arithmetic
operations and conversions involving integral types. The checked statement causes all expressions to be
evaluated in a checked context, and the unchecked statement causes all expressions to be evaluated in an
unchecked context.
1.7.16 The lock statement
The lock statement obtains the mutual-exclusion lock for a given object, executes a statement, and then
releases the lock.
1.8 Classes
Class declarations are used to define new reference types. C# supports single inheritance only, but a class may
implement multiple interfaces.
Class members can include constants, fields, methods, properties, indexers, events, operators, constructors,
destructors, and nested type declaration.
Each member of a class has a form of accessibility. There are five forms of accessibility:
" public members are available to all code;
" protected members are accessible only from derived classes;
" internal members are accessible only from within the same assembly;
" protected internal members are accessible only from derived classes within the same assembly;
" private members are accessible only from the class itself.
14 Copyright Microsoft Corporation 1999-2000. All Rights Reserved.
Chapter 1 Introduction
1.9 Structs
The list of similarities between classes and structs is long  structs can implement interfaces, and can have the
same kinds of members as classes. Structs differ from classes in several important ways, however: structs are [ Pobierz całość w formacie PDF ]
  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • akte20.pev.pl