Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Лаба №1 / books / csharp_ebook.pdf
Скачиваний:
48
Добавлен:
03.03.2016
Размер:
3.69 Mб
Скачать

Programmers Heaven: C# School

ulong

UInt64

8

 

Unsigned, ranges from 0 to 18,446,744,073,709,551,615

 

 

 

 

 

 

 

 

Floating Point Types

 

 

 

 

 

float

Single

4

 

Ranges from ±1.5 × 10-45 to ±3.4 × 1038 with 7 digits

 

 

 

 

precision. Requires the suffix 'f' or 'F'

double

Double

8

 

Ranges from ±5.0 × 10-324 to ±1.7 × 10308 with 15-16

(default)

 

 

 

digits

 

 

 

 

Precision

 

 

 

Other Types

 

 

 

 

 

bool

Boolean

1

 

Contains either true or false

 

 

 

 

 

char

Char

2

 

Contains any single Unicode character enclosed in single

 

 

 

 

quotation mark such as 'c'

decimal

Decimal

12

 

Ranges from 1.0 × 10-28 to 7.9 × 1028 with 28-29 digits

 

 

 

 

precision. Requires the suffix 'm' or 'M'

Implicit data types are represented in language using keywords, so each of the above is a keyword in C# (Keyword are the words defined by the language and can not be used as identifiers). It is worth noting that string is also an implicit data type in C#, so string is a keyword in C#. The last point about implicit data types is that they are value types and thus stored on the stack, while user defined types or referenced types are stored using the heap. A stack is a data structure that store items in a first in first out (FIFO) fashion. It is an area of memory supported by the processor and its size is determined at the compile time. A heap consists of memory available to the program at run time. Reference types are allocated using memory available from the heap dynamically (during the execution of program). The garbage collector searches for non-referenced data in heap during the execution of program and returns that space to Operating System.

Variables

During the execution of a program, data is temporarily stored in memory. A variable is the name given to a memory location holding a particular type of data. So, each variable has associated with it a data type and a value. In C#, variables are declared as:

<data type> <variable>;

e.g.,

int i;

The above line will reserve an area of 4 bytes in memory to store an integer type values, which will be referred to in the rest of program by the identifier 'i'. You can initialize the variable as you declare it (on the fly) and can also declare/initialize multiple variables of the same type in a single statement, e.g.,

32

Programmers Heaven: C# School

bool

isReady

= true;

float percentage

= 87.88, average = 43.9;

char

digit

= '7';

 

 

 

In C# (like other modern languages), you must declare variables before using them. Also, there is the concept of "Definite Assignment" in C# which says "local variables (variables defined in a method) must be initialized before being used". The following program won't compile:

static void Main()

 

{

 

int age;

 

// age = 18;

 

Console.WriteLine(age);

// error

}

 

But, if you un-comment the 2nd line, the program will compile. C# does not assign default values to local variables. C# is also a type safe language, i.e., values of particular data type can only be stored in their respective (or compatible) data type. You can't store integer values in Boolean data types like we used to do in C/C++.

Constant Variables or Symbols

Constants are variables whose values, once defined, can not be changed by the program. Constant variables are declared using the const keyword, like:

const double PI = 3.142;

Constant variables must be initialized as they are declared. It is a syntax error to write:

const int MARKS;

It is conventional to use capital letters when naming constant variables.

Naming Conventions for variables and methods

Microsoft suggests using Camel Notation (first letter in lowercase) for variables and Pascal Notation (first letter in uppercase) for methods. Each word after the first word in the name of both variables and methods should start with a capital letter. For example, variable names following Camel notation could be:

salary

totalSalary

myMathsMarks isPaid

Some typical names of method following Pascal Notation are

33

Соседние файлы в папке books