Three common ways of packaging code are methods, classes and namespaces. C# applications are written by combining new methods and classes that you write with predefined methods and classes available in the .NET Framework Class Library (also referred to as the FCL) and in various other class libraries. Related classes are often grouped into namespaces and compiled into class libraries so that they can be reused in other applications. You will learn how to create your own namespaces and class libraries in Chapter 9. The FCL provides many predefined classes that contain methods for performing common mathematical calculations, string manipulations, character manipulations, input/output operations, database operations, networking operations, file processing, error checking and many other useful operations.
|
Methods (called functions or procedures in other programming languages) allow you to modularize an application by separating its tasks into self-contained units. You have declared methods in every application you have written. These methods are sometimes referred to as user-defined methods. The actual statements in the method bodies are written only once, can be reused from several locations in an application and are hidden from other methods.
There are several motivations for modularizing an application by means of methods. One is the "divide-and-conquer" approach, which makes application development more manageable by constructing applications from small, simple pieces. Another is software reusabilityexisting methods can be used as building blocks to create new applications. Often, you can create applications mostly by reusing existing methods rather than by building customized code. For example, in earlier applications, we did not have to define how to read data values from the keyboardthe FCL provides these capabilities in class Console. A third motivation is to avoid repeating code. Dividing an application into meaningful methods makes the application easier to debug and maintain.
|
|
|
As you know, a method is invoked by a method call, and when the called method completes its task, it either returns a result or simply control to the caller. An analogy to this application structure is the hierarchical form of management (Figure 7.1). A boss (the caller) asks a worker (the called method) to perform a task and report back (i.e., return) the results after completing the task. The boss method does not know how the worker method performs its designated tasks. The worker may also call other worker methods, unbeknownst to the boss. This "hiding" of implementation details promotes good software engineering. Figure 7.1 shows the boss method communicating with several worker methods in a hierarchical manner. The boss method divides the responsibilities among the various worker methods. Note that worker1 acts as a "boss method" to worker4 and worker5.
Figure 7.1. Hierarchical boss-method/worker-method relationship.
Preface
Index
Introduction to Computers, the Internet and Visual C#
Introduction to the Visual C# 2005 Express Edition IDE
Introduction to C# Applications
Introduction to Classes and Objects
Control Statements: Part 1
Control Statements: Part 2
Methods: A Deeper Look
Arrays
Classes and Objects: A Deeper Look
Object-Oriented Programming: Inheritance
Polymorphism, Interfaces & Operator Overloading
Exception Handling
Graphical User Interface Concepts: Part 1
Graphical User Interface Concepts: Part 2
Multithreading
Strings, Characters and Regular Expressions
Graphics and Multimedia
Files and Streams
Extensible Markup Language (XML)
Database, SQL and ADO.NET
ASP.NET 2.0, Web Forms and Web Controls
Web Services
Networking: Streams-Based Sockets and Datagrams
Searching and Sorting
Data Structures
Generics
Collections
Appendix A. Operator Precedence Chart
Appendix B. Number Systems
Appendix C. Using the Visual Studio 2005 Debugger
Appendix D. ASCII Character Set
Appendix E. Unicode®
Appendix F. Introduction to XHTML: Part 1
Appendix G. Introduction to XHTML: Part 2
Appendix H. HTML/XHTML Special Characters
Appendix I. HTML/XHTML Colors
Appendix J. ATM Case Study Code
Appendix K. UML 2: Additional Diagram Types
Appendix L. Simple Types
Index