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

C H A P T E R 3

Types, Storage, and Variables

A C# Program Is a Set of Type Declarations

A Type Is a Template

Instantiating a Type

Data Members and Function Members

Predefined Types

User-Defined Types

The Stack and the Heap

Value Types and Reference Types

Variables

Static Typing and the dynamic Keyword

Nullable Types

31

CHAPTER 3 TYPES, STORAGE, AND VARIABLES

A C# Program Is a Set of Type Declarations

If you were to broadly characterize the source code of C and C++ programs, you might say that a C program is a set of functions and data types and that a C++ program is a set of functions and classes. A C# program, however, is a set of type declarations.

The source code of a C# program or DLL is a set of one or more type declarations.

For an executable, one of the types declared must be a class that includes a method called Main.

A namespace is a way of grouping a related set of type declarations and giving the group a name. Since your program is a related set of type declarations, you will generally declare your program type inside a namespace you create.

For example, the following code shows a program that consists of three type declarations. The three types are declared inside a new namespace called MyProgram.

namespace MyProgram

// Create a new namespace.

{

 

DeclarationOfTypeA

// Declare a type.

DeclarationOfTypeB

// Declare a type.

class C

// Declare a type.

{

 

static void Main()

 

{

 

...

 

}

 

}

 

}

 

Namespaces are covered in more detail in Chapter 10.

32

CHAPTER 3 TYPES, STORAGE, AND VARIABLES

A Type Is a Template

Since a C# program is just a set of type declarations, learning C# consists of learning how to create and use types. So, the first thing you need to do is to look at what a type is.

You can start by thinking of a type as a template for creating data structures. It isn’t the data structure itself, but it specifies the characteristics of objects constructed from the template.

A type is defined by the following elements:

A name

A data structure to contain its data members

Behaviors and constraints

For example, Figure 3-1 illustrates the components of two types: short and int.

Figure 3-1. A type is a template.

Instantiating a Type

Creating an actual object from the type’s template is called instantiating the type.

The object created by instantiating a type is called either an object of the type or an instance of the type. The terms are interchangeable.

Every data item in a C# program is an instance of some type—a type either provided by the language, provided by the BCL or another library, or defined by the programmer.

Figure 3-2 illustrates the instantiation of objects of two predefined types.

Figure 3-2. Instantiating a type creates an instance.

33

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