Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
CSharp_for_Beginners.doc
Скачиваний:
35
Добавлен:
13.02.2015
Размер:
2.39 Mб
Скачать

Using Built-in Data Types

Built-in data types are used within a C# program in several ways.

As variables:

int answer = 42;

string greeting = "Hello, World!";

As constants:

const int speedLimit = 55;

const double pi = 3.14159265358979323846264338327950;

As return values and parameters:

long CalculateSum(int a, int b)

{

long result = a + b;

return result;

}

To define your own data types, use Classes, Enumerations or Structs.

Converting Data Types

Converting between data types can be done implicitly, in which the conversion is done automatically by the compiler, or explicitly using a cast, in which the programmer forces the conversion, and assumes the risk of losing information.

For example:

int i = 0;

double d = 0;

i = 10;

d = i; // An implicit conversion

d = 3.5;

i = (int) d; // An explicit conversion, or "cast"

Использование встроенных типов данных

Встроенные типы данных используются в программах C# несколькими способами.

Как переменные.

int answer = 42;

string greeting = "Hello, World!";

Как константы.

const int speedLimit = 55;

const double pi = 3.14159265358979323846264338327950;

Как возвращаемые значения и параметры.

----

Можно также определять собственные типы данных, используя Классы, Перечисления или Структуры.

Преобразование типов данных

Преобразование между типами данных можно сделать неявно, в этом случае преобразование автоматически выполняет компилятор, или явно с помощью приведения, в этом случае принудительное преобразование выполняет программист, учитывая риск возможной потери данных.

Пример.

--------

Value and Reference Types

Unlike some programming languages you might be familiar with, C# has two varieties of data types: value and reference. It's important to know the difference if performance is important to your application, or if you are interested in how C# manages data and memory.

When a variable is declared using one of the basic, built-in data types or a user defined structure, it is a value type. An exception is the string data type, which is a reference type.

A value type stores its contents in memory allocated on the stack. For example, in this case the value 42 is stored in an area of memory called the stack.:

int x = 42;

When the variable x goes out of scope because the method in which it was defined has finished executing, the value is discarded from the stack.

Using the stack is efficient, but the limited lifetime of value types makes them less suited for sharing data between different classes.

In contrast, a reference type, such as an instance of a class or an array, is allocated in a different area of memory called the heap. In the example below, the space required for the ten integers that make up the array is allocated on the heap.

int[] numbers = new int[10];

This memory isn't returned to the heap when a method finishes; it's only reclaimed when C#'s garbage collection system determines it is no longer needed. There is a greater overhead in declaring reference types, but they have the advantage of being accessible from other classes.

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