Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C# 2008 Step by Step.pdf
Скачиваний:
27
Добавлен:
25.03.2016
Размер:
14 Мб
Скачать
☆

120 Part I Introducing Microsoft Visual C# and Microsoft Visual Studio 2008

If you want to exit Visual Studio 2008 now

On the File menu, click Exit. If you see a Save dialog box, click Yes (if you are using Visual Studio 2008) or Save (if you are using Visual C# 2008 Express Edition) and save the project.

Chapter 6 Quick Reference

To

Do this

Throw an exception

Use a throw statement. For example:

 

throw new FormatException(source);

Ensure that integer arithmetic

Use the checked keyword. For example:

is always checked for overflow

int number = Int32.MaxValue;

 

 

checked

 

{

 

number++;

 

}

Catch a specific exception

Write a catch handler that catches the specific exception class. For

 

example:

 

try

 

{

 

...

 

}

 

catch (FormatException fEx)

 

{

 

...

 

}

Catch all exceptions in a single catch handler

Write a catch handler that catches Exception. For example:

try

{

...

}

catch (Exception ex)

{

...

}

Ensure that some code will always be run, even if an exception is thrown

Write the code inside a finally block. For example:

try

{

...

}

finally

{

// always run

}

Microsoft Visual C# 2008 Step by Step

Part II

Understanding the C# Language

In this part:

Chapter 7. Creating and Managing Classes and Objects . . . . . . . . . . . . . . . . . . . 123 Chapter 8. Understanding Values and References . . . . . . . . . . . . . . . . . . . . . . . . 145 Chapter 9. Creating Value Types with Enumerations and Structures . . . . . . . . . 167 Chapter 10. Using Arrays and Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185 Chapter 11. Understanding Parameter Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207 Chapter 12. Working with Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217 Chapter 13. Creating Interfaces and Defining Abstract Classes . . . . . . . . . . . . . 239 Chapter 14. Using Garbage Collection and Resource Management . . . . . . . . . 257

121

Chapter 7

Creating and Managing Classes

and Objects

After completing this chapter, you will be able to:

Define a class containing a related set of methods and data items.

Control the accessibility of members by using the public and private keywords.

Create objects by using the new keyword to invoke a constructor.

Write and call your own constructors.

Create methods and data that can be shared by all instances of the same class by using the static keyword.

In Part I, “Introducing Microsoft Visual C# and Microsoft Visual Studio 2008,” you learned how to declare variables, use operators to create values, call methods, and write many of the statements you need when implementing a method. You now know enough to progress to the next stage—combining methods and data into your own classes.

The Microsoft .NET Framework contains thousands of classes, and you have used a number of them already, including Console and Exception. Classes provide a convenient mechanism

for modeling the entities manipulated by applications. An entity can represent a specific item, such as a customer, or something more abstract, such as a transaction. Part of the design process of any system is concerned with determining the entities that are important and then performing an analysis to see what information they need to hold and what functions they should perform. You store the information that a class holds as fields and use methods to implement the operations that a class can perform.

The chapters in Part II, “Understanding the C# Language,” provide you with all you need to know to create your own classes.

Understanding Classification

Class is the root word of the term classification. When you design a class, you systematically arrange information into a meaningful entity. This arranging is an act of classification and is something that everyone does—not just programmers. For example, all cars share common behaviors (they can be steered, stopped, accelerated, and so on) and common attributes (they have a steering wheel, an engine, and so on). People use the word car to mean

123

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