Содержание

Глава 2

Обзор элементов языка C#

  1. This is a simple C# program. Call this program Example.cs.
  2. This version does not include the using System statement.
  3. This program demonstrates variables.
  4. This program illustrates the differences between int and double.
  5. Compute the area of a circle.
  6. Demonstrate the if.
  7. Demonstrate the for loop.
  8. Demonstrate a block of code.
  9. Compute the sum and product of the numbers from 1 to 10.
  10. Demonstrate an @ identier.

listing 1
/*
This is a simple C# program.

Call this program Example.cs.
*/

using System;

class Example {
// A C# program begins with a call to Main().
public static void Main() {
Console.WriteLine("A simple C# program.");
}
}


Результат:

A simple C# program.

Анализ:

Вверх


listing 2
// This version does not include the using System statement.

class Example {
// A C# program begins with a call to Main().
public static void Main() {
// Here, Console.WriteLine is fully qualified.
System.Console.WriteLine("A simple C# program.");
}
}


Результат:

A simple C# program.

Анализ:

Вверх


listing 3
// This program demonstrates variables.

using System;

class Example2 {
public static void Main() {
int x; // this declares a variable
int y; // this declares another variable

x = 100; // this assigns 100 to x

Console.WriteLine("x contains " + x);

y = x / 2;

Console.Write("y contains x / 2: ");
Console.WriteLine(y);
}
}

Результат:

x contains 100
y contains x / 2: 50

Анализ:

Вверх


listing 4
/*
This program illustrates the differences
between int and double.
*/

using System;

class Example3 {

public static void Main() {
int ivar; // this declares an int variable
double dvar; // this declares a floating-point variable

ivar = 100; // assign ivar the value 100

dvar = 100.0; // assign dvar the value 100.0

Console.WriteLine("Original value of ivar: " + ivar);
Console.WriteLine("Original value of dvar: " + dvar);

Console.WriteLine(); // print a blank line

// now, divide both by 3
ivar = ivar / 3;
dvar = dvar / 3.0;

Console.WriteLine("ivar after division: " + ivar);
Console.WriteLine("dvar after division: " + dvar);
}
}


Результат:

Original value of ivar: 100
Original value of dvar: 100

ivar after division: 33
dvar after division: 33,3333333333333

Анализ:

Вверх


listing 5
// Compute the area of a circle.

using System;

class Circle {
static void Main() {
double radius;
double area;

radius = 10.0;
area = radius * radius * 3.1416;

Console.WriteLine("Area is " + area);

}
}


Результат:

Area is 314,16

Анализ:

Вверх


listing 6
// Demonstrate the if.

using System;

class IfDemo {
public static void Main() {
int a, b, c;

a = 2;
b = 3;

if(a < b) Console.WriteLine("a is less than b");

// this won't display anything
if(a == b) Console.WriteLine("you won't see this");

Console.WriteLine();

c = a - b; // c contains -1

Console.WriteLine("c contains -1");
if(c >= 0) Console.WriteLine("c is non-negative");
if(c < 0) Console.WriteLine("c is negative");

Console.WriteLine();

c = b - a; // c now contains 1
Console.WriteLine("c contains 1");
if(c >= 0) Console.WriteLine("c is non-negative");
if(c < 0) Console.WriteLine("c is negative");

}
}


Результат:

a is less than b

c contains -1
c is negative

c contains 1
c is non-negative

Анализ:

Вверх


listing 7
// Demonstrate the for loop.

using System;

class ForDemo {
public static void Main() {
int count;

for(count = 0; count < 5; count = count+1)
Console.WriteLine("This is count: " + count);

Console.WriteLine("Done!");

}
}


Результат:

This is count: 0
This is count: 1
This is count: 2
This is count: 3
This is count: 4

Анализ:

Вверх


listing 8
// Demonstrate a block of code.

using System;

class BlockDemo {
public static void Main() {
int i, j, d;

i = 5;
j = 10;

// the target of this if is a block
if(i != 0) {

Console.WriteLine("i does not equal zero");
d = j / i;
Console.WriteLine("j / i is " + d);
}
}
}


Результат:

i does not equal zero
j / i is 2

Анализ:

Вверх


listing 9
// Compute the sum and product of the numbers from 1 to 10.

using System;

class ProdSum {
static void Main() {
int prod;
int sum;
int i;

sum = 0;
prod = 1;

for(i=1; i <= 10; i++) {

sum = sum + i;
prod = prod * i;
}
Console.WriteLine("Sum is " + sum);
Console.WriteLine("Product is " + prod);

}
}

Результат:

Sum is 55
Product is 3628800

Анализ:

Вверх


listing 10
// Demonstrate an @ identier.

using System;

class IdTest {

static void Main() {
int @if; // use if as an identifier

for(@if = 0; @if < 10; @if++)
Console.WriteLine("@if is " + @if);
}
}


Результат:

@if is 0
@if is 1
@if is 2
@if is 3
@if is 4
@if is 5
@if is 6
@if is 7
@if is 8
@if is 9

Анализ:



Содержание

Hosted by uCoz