Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Daniel Solis - Illustrated C# 2010 - 2010.pdf
Скачиваний:
19
Добавлен:
11.06.2015
Размер:
11.23 Mб
Скачать

CHAPTER 2 OVERVIEW OF C# PROGRAMMING

Naming Conventions

The C# Language Specification suggests that certain casing conventions be used in creating identifiers. Table 2-2 summarizes the suggested guidelines for casing.

For most type identifiers, the Pascal casing style is recommended. In this style, each of the words combined to make an identifier is capitalized—for example, FirstName and LastName.

Table 2-2. Recommended Identifier Naming Styles

Style Name

Description

Recommended Use

Examples

Pascal casing

Each word in the

Use for type names and

CardDeck, DealersHand

 

identifier is capitalized.

member names.

 

Camel casing

Each word in the

Use for local variables and

totalCycleCount,

 

identifier, except the

method parameters.

randomSeedParam

 

first, is capitalized.

 

 

Uppercase

The identifier is

Use only for abbreviations.

IO, DMA, XML

 

composed of all

 

 

 

uppercase letters.

 

 

 

 

 

 

Although these are the suggested guidelines, many organizations use other conventions— particularly in the naming of member fields, which I’ll introduce in the next chapter. Two of the common conventions are the following:

Begin a field name with an underscore: _highTemp, _lowTemp

Begin a field name with m_: m_highTemp, m_lowTemp

Both of these methods have the advantage of showing you immediately that these identifiers are field names. These forms also allow Visual Studio’s IntelliSense feature to group all the fields together in the pop-ups.

19

CHAPTER 2 OVERVIEW OF C# PROGRAMMING

Keywords

Keywords are the character string tokens used to define the C# language. Table 2-3 gives a complete list of the C# keywords.

Some important things to know about keywords are the following:

Keywords cannot be used as variable names or any other form of identifier, unless prefaced with the @ character.

All C# keywords consist entirely of lowercase letters. (.NET type names, however, use Pascal casing.)

Table 2-3. The C# Keywords

abstract

const

extern

int

out

short

typeof

as

continue

false

interface

override

sizeof

uint

base

decimal

finally

internal

params

stackalloc

ulong

bool

default

fixed

is

private

static

unchecked

break

delegate

float

lock

protected

string

unsafe

Byte

do

for

long

public

struct

ushort

case

double

foreach

namespace

readonly

switch

using

catch

else

goto

new

ref

this

virtual

char

enum

if

null

return

throw

void

checked

event

implicit

object

sbyte

true

volatile

class

explicit

in

operator

sealed

try

while

 

 

 

 

 

 

 

Contextual keywords are identifiers that act as keywords only in certain language constructs. In those positions, they have particular meanings; but unlike keywords, which cannot ever be used as identifiers, contextual keywords can be used as identifiers in other parts of the code. Table 2-4 contains the list of contextual keywords.

Table 2-4. The C# Contextual Keywords

add

ascending

by

descending

dynamic

equals

from

get

global

group

into

join

let

on

orderby

partial

remove

select

set

value

var

where

yield

 

 

 

 

 

 

 

 

 

 

 

 

20

CHAPTER 2 OVERVIEW OF C# PROGRAMMING

Main: The Starting Point of a Program

Every C# program must have one class with a method (function) called Main. In the SimpleProgram program shown previously, it was declared in a class called Program.

The starting point of execution of every C# program is at the first instruction in Main.

The name Main must be capitalized.

The simplest form of Main is the following:

static void Main( )

{

Statements

}

Whitespace

Whitespace in a program refers to characters that do not have a visible output character. Whitespace in source code is ignored by the compiler, but is used by the programmer to make the code clearer and easier to read. Some of the whitespace characters include the following:

Space

Tab

New line

Carriage return

For example, the following code fragments are treated exactly the same by the compiler in spite of their differences in appearance.

//Nicely formatted Main()

{

Console.WriteLine("Hi, there!");

}

//Just concatenated Main(){Console.WriteLine("Hi, there!");}

21

CHAPTER 2 OVERVIEW OF C# PROGRAMMING

Statements

The statements in C# are very similar to those of C and C++. This section introduces the general form of statements; the specific statement forms are covered in Chapter 9.

Simple Statements

A statement is a source code instruction describing a type or telling the program to perform an action.

A simple statement is terminated by a semicolon.

For example, the following code is a sequence of two simple statements. The first statement defines an integer variable named var1 and initializes its value to 5. The second statement prints the value of variable var1 to a window on the screen.

int var1 = 5;

System.Console.WriteLine("The value of var1 is {0}", var1);

Blocks

A block is a sequence of zero or more statements enclosed by a matching set of curly braces; it acts as a single syntactic statement.

You can create a block from the set of two statements in the preceding example by enclosing the statements in matching curly braces, as shown in the following code:

{

int var1 = 5;

System.Console.WriteLine("The value of var1 is {0}", var1);

}

Some important things to know about blocks are the following:

You can use a block whenever the syntax requires a statement but the action you need requires more than one simple statement.

Certain program constructs require blocks. In these constructs, you cannot substitute a simple statement for the block.

Although a simple statement is terminated by a semicolon, a block is not followed by a semicolon. (Actually, the compiler will allow it—but it’s not good style.)

{Terminating semicolon

Terminating semicolon

int var2 = 5;

System.Console.WriteLine("The value of var1 is {0}", var1);

}

↑ No terminating semicolon

22

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]