- •About the Authors
- •Contents at a Glance
- •Contents
- •Introduction
- •Goal of the Book
- •How to Use this Book
- •Introduction to the .NET Framework
- •Common Language Runtime (CLR)
- •Class Library
- •Assembly
- •Versioning
- •Exceptions
- •Threads
- •Delegates
- •Summary
- •Introduction to C#
- •Variables
- •Initializing Variables
- •Variable Modifiers
- •Variable Data Types
- •Types of Variables
- •Variable Scope
- •Types of Data Type Casting
- •Arrays
- •Strings
- •Initializing Strings
- •Working with Strings
- •Statements and Expressions
- •Types of Statements
- •Expressions
- •Summary
- •Classes
- •Declaring Classes
- •Inheritance
- •Constructors
- •Destructors
- •Methods
- •Declaring a Method
- •Calling a Method
- •Passing Parameters to Methods
- •Method Modifiers
- •Overloading a Method
- •Namespaces
- •Declaring Namespaces
- •Aliases
- •Structs
- •Enumerations
- •Interfaces
- •Writing, Compiling, and Executing
- •Writing a C# Program
- •Compiling a C# Program
- •Executing a C# Program
- •Summary
- •Arrays
- •Single-Dimensional Arrays
- •Multidimensional Arrays
- •Methods in Arrays
- •Collections
- •Creating Collections
- •Working with Collections
- •Indexers
- •Boxing and Unboxing
- •Preprocessor Directives
- •Summary
- •Attributes
- •Declaring Attributes
- •Attribute Class
- •Attribute Parameters
- •Default Attributes
- •Properties
- •Declaring Properties
- •Accessors
- •Types of Properties
- •Summary
- •Introduction to Threads
- •Creating Threads
- •Aborting Threads
- •Joining Threads
- •Suspending Threads
- •Making Threads Sleep
- •Thread States
- •Thread Priorities
- •Synchronization
- •Summary
- •Case Study
- •Project Life Cycle
- •Analyzing Requirements
- •High-Level Design
- •Primary and Foreign Keys
- •Referential Integrity
- •Normalization
- •Designing a Database
- •Low-Level Design
- •Construction
- •Integration and Testing
- •User Acceptance Testing
- •Implementation
- •Operations and Maintenance
- •Summary
- •Creating a New Project
- •Console Application
- •Windows Applications
- •Creating a Windows Application for the Customer Maintenance Project
- •Creating an Interface for Form1
- •Creating an Interface for WorkerForm
- •Creating an Interface for CustomerForm
- •Creating an Interface for ReportsForm
- •Creating an Interface for JobDetailsForm
- •Summary
- •Performing Validations
- •Identifying the Validation Mechanism
- •Using the ErrorProvider Control
- •Handling Exceptions
- •Using the try and catch Statements
- •Using the Debug and Trace Classes
- •Using the Debugging Features of Visual Studio .NET
- •Using the Task List
- •Summary
- •Creating Form1
- •Connecting WorkerForm to the Workers Table
- •Connecting CustomerForm to the tblCustomer Table
- •Connecting the JobDetails Form
- •to the tblJobDetails Table
- •Summary
- •Introduction to the Crystal Reports Designer Tool
- •Creating the Reports Form
- •Creating Crystal Reports
- •Creating the Windows Forms Viewer Control
- •Creating the Monthly Worker Report
- •Summary
- •Introduction to Deploying a Windows Application
- •Deployment Projects Available in Visual Studio .NET
- •Deployment Project Editors
- •Summary
- •Case Study
- •Project Life Cycle
- •Analyzing Requirements
- •High-Level Design
- •Low-Level Design
- •Summary
- •Populating the TreeView Control
- •Displaying Employee Codes in the TreeView Control
- •Event Handling
- •Displaying Employee Details in the ListView Control
- •Summary
- •Case Study
- •Project Life Cycle
- •Analyzing Requirements
- •High-Level Design
- •Low-Level Design
- •Summary
- •Adding the Programming Logic to the Application
- •Adding Code to the Form Load() Method
- •Adding Code to the OK Button
- •Adding Code to the Exit Button
- •Summary
- •The Created Event
- •Adding Code to the Created Event
- •Overview of XML
- •The XmlReader Class
- •The XmlWriter Class
- •Displaying Data in an XML Document
- •Displaying an Error Message in the Event Log
- •Displaying Event Entries from Event Viewer
- •Displaying Data from the Summary.xml Document in a Message Box
- •Summary
- •Airline Profile
- •Role of a Business Manager
- •Role of a Network Administrator
- •Role of a Line-of-Business Executive
- •Project Requirements
- •Creation and Deletion of User Accounts
- •Addition of Flight Details
- •Reservations
- •Cancellations
- •Query of Status
- •Confirmation of Tickets
- •Creation of Reports
- •Launch of Frequent Flier Programs
- •Summarizing the Tasks
- •Project Design
- •Database Design
- •Web Forms Design
- •Enabling Security with the Directory Structure
- •Summary
- •Getting Started with ASP.NET
- •Prerequisites for ASP.NET Applications
- •New Features in ASP.NET
- •Types of ASP.NET Applications
- •Exploring ASP.NET Web Applications
- •Introducing Web Forms
- •Web Form Server Controls
- •Configuring ASP.NET Applications
- •Configuring Security for ASP.NET Applications
- •Deploying ASP.NET Applications
- •Creating a Sample ASP.NET Application
- •Creating a New Project
- •Adding Controls to the Project
- •Coding the Application
- •Summary
- •Creating the Database Schema
- •Creating Database Tables
- •Managing Primary Keys and Relationships
- •Viewing the Database Schema
- •Designing Application Forms
- •Standardizing the Interface of the Application
- •Common Forms in the Application
- •Forms for Network Administrators
- •Forms for Business Managers
- •Forms for Line-of-Business Executives
- •Summary
- •The Default.aspx Form
- •The Logoff.aspx Form
- •The ManageUsers.aspx Form
- •The ManageDatabases.aspx Form
- •The ChangePassword.aspx Form
- •Restricting Access to Web Forms
- •The AddFl.aspx Form
- •The RequestID.aspx Form
- •The Reports.aspx Form
- •The FreqFl.aspx Form
- •Coding the Forms for LOB Executives
- •The CreateRes.aspx Form
- •The CancelRes.aspx Form
- •The QueryStat.aspx Form
- •The ConfirmRes.aspx Form
- •Summary
- •Designing the Form
- •The View New Flights Option
- •The View Ticket Status Option
- •The View Flight Status Option
- •The Confirm Reservation Option
- •Testing the Application
- •Summary
- •Locating Errors in Programs
- •Watch Window
- •Locals Window
- •Call Stack Window
- •Autos Window
- •Command Window
- •Testing the Application
- •Summary
- •Managing the Databases
- •Backing Up the SkyShark Airlines Databases
- •Exporting Data from Databases
- •Examining Database Logs
- •Scheduling Database Maintenance Tasks
- •Managing Internet Information Server
- •Configuring IIS Error Pages
- •Managing Web Server Log Files
- •Summary
- •Authentication Mechanisms
- •Securing a Web Site with IIS and ASP.NET
- •Configuring IIS Authentication
- •Configuring Authentication in ASP.NET
- •Securing SQL Server
- •Summary
- •Deployment Scenarios
- •Deployment Editors
- •Creating a Deployment Project
- •Adding the Output of SkySharkDeploy to the Deployment Project
- •Deploying the Project to a Web Server on Another Computer
- •Summary
- •Organization Profile
- •Project Requirements
- •Querying for Information about All Books
- •Querying for Information about Books Based on Criteria
- •Ordering a Book on the Web Site
- •Project Design
- •Database Design
- •Database Schema
- •Web Forms Design
- •Flowcharts for the Web Forms Modules
- •Summary
- •Introduction to ASP.NET Web Services
- •Web Service Architecture
- •Working of a Web Service
- •Technologies Used in Web Services
- •XML in a Web Service
- •WSDL in a Web Service
- •SOAP in a Web Service
- •UDDI in a Web Service
- •Web Services in the .NET Framework
- •The Default Code Generated for a Web Service
- •Testing the SampleWebService Web Service
- •Summary
- •Creating the SearchAll() Web Method
- •Creating the SrchISBN() Web Method
- •Creating the AcceptDetails() Web Method
- •Creating the GenerateOrder() Web Method
- •Testing the Web Service
- •Securing a Web Service
- •Summary
- •Creating the Web Forms for the Bookers Paradise Web Site
- •Adding Code to the Web Forms
- •Summary
- •Case Study
- •Project Life Cycle
- •Analyzing Requirements
- •High-Level Design
- •Low-Level Design
- •Summary
- •Overview of Mobile Applications
- •The Microsoft Mobile Internet Toolkit
- •Overview of WAP
- •The WAP Architecture
- •Overview of WML
- •The Mobile Web Form
- •The Design of the MobileTimeRetriever Application
- •Creating the Interface for the Mobile Web Forms
- •Adding Code to the MobileTimeRetriever Application
- •Summary
- •Creating the Forms Required for the MobileCallStatus Application
- •Creating the frmLogon Form
- •Creating the frmSelectOption Form
- •Creating the frmPending Form
- •Creating the frmUnattended Form
- •Adding Code to the Submit Button in the frmLogon Form
- •Adding Code to the Query Button in the frmSelectOption Form
- •Adding Code to the Mark checked as complete Button in the frmPending Form
- •Adding Code to the Back Button in the frmPending Form
- •Adding Code to the Accept checked call(s) Button in the frmUnattended Form
- •Adding Code to the Back Button in the frmUnattended Form
- •Summary
- •What Is COM?
- •Windows DNA
- •Microsoft Transaction Server (MTS)
- •.NET Interoperability
- •COM Interoperability
- •Messaging
- •Benefits of Message Queues
- •Limitations
- •Key Messaging Terms
- •Summary
- •Pointers
- •Declaring Pointers
- •Types of Code
- •Implementing Pointers
- •Using Pointers with Managed Code
- •Working with Pointers
- •Compiling Unsafe Code
- •Summary
- •Introduction to the Languages of Visual Studio .NET
- •Visual C# .NET
- •Visual Basic .NET
- •Visual C++ .NET
- •Overview of Visual Basic .NET
- •Abstraction
- •Encapsulation
- •Inheritance
- •Polymorphism
- •Components of Visual Basic .NET
- •Variables
- •Constants
- •Operators
- •Arrays
- •Collections
- •Procedures
- •Arguments
- •Functions
- •Adding Code to the Submit Button
- •Adding Code to the Exit Button
- •Summary
- •Introduction to Visual Studio .NET IDE
- •Menu Bar
- •Toolbars
- •Visual Studio .NET IDE Windows
- •Toolbox
- •The Task List Window
- •Managing Windows
- •Customizing Visual Studio .NET IDE
- •The Options Dialog Box
- •The Customize Dialog Box
- •Summary
- •Index
4Part I INTRODUCTION TO C#
Microsoft has released the Visual C# language and its framework—called the
.NET Framework—with the aim of providing programmers with complete support for developing applications. This chapter introduces you to the .NET Framework and its components. These components include the CLR (common language runtime) and the .NET base class library. In addition, this chapter introduces you to CTS (common type system), CLS (common language specification), garbage collector, and assembly. Subsequently, the chapter details some of the base classes of C#, such as Delegate, Exception, and Thread, included in the .NET Framework.
Introduction to the .NET Framework
The .NET Framework is a new API (application programming interface) that helps programmers to write applications for the Windows platform. In addition, .NET enables you to write programs or applications for a distributed environment. To do so, .NET helps you to create Web services and Web applications. You will learn about Web services and Web applications later in this chapter.
The .NET Framework not only helps you write new programs, but also provides you with the ability to improve the existing programs. The .NET Framework is well designed for communicating with existing COM (Component Object Mode l ) components, making the applications written in .NET languages backwardcompatible with existing programs.
The .NET Framework provides a complete development framework. The .NET Framework helps you write complex applications by providing you with predefined classes and methods in the .NET base class library and also manages the execution of the applications that you write.
.NET provides you with a library of classes, which contains several base classes called .NET base classes. These classes, in turn, define several functions that you can use to write your applications. In addition,.NET provides the .NET Runtime environment called the CLR to execute the code written for the Windows platform. You will learn about the .NET class library and the .NET Runtime environment in detail later in this chapter.
OVERVIEW OF THE .NET FRAMEWORK |
Chapter 1 |
5 |
|
|
|
.NET offers an application development environment called Visual Studio .NET that consists of several programming languages, such as Visual Basic .NET, Visual C#, Visual FoxPro, and Visual C++ .NET. These programming languages combine the features of the existing languages with several new features to provide a powerful development framework. Following are some of the features of the
.NET Framework.
Interoperability with other environments. The need for a new development environment was primarily because the applications developed in existing environments were not platform-independent. For example, applications that you develop for the Windows platform are not compatible with the applications designed for the UNIX environment. With the evolution of the .NET Framework, you can develop applications that can run on the Internet, making them accessible across various platforms. These applications are called Web applications and are fully supported by the .NET Framework.
Interoperability across environments, which is the strongest feature of
.NET applications, is a result of .NET’s support for MSIL (Microsoft intermediate language). At the time of compilation, all the managed code written for the .NET platform is converted to MSIL, which is a set of CPU-independent instructions. When you run the code written for the Windows platform in any other environment, for example UNIX, the compiler compiles the MSIL code to one that UNIX understands, enabling the application to run.
Support for developing language-independent applications. In addition to developing applications that can interoperate with those in
a different environment, you can develop applications that are languageindependent. Visual Studio .NET provides a common development environment for all languages in the .NET series. This implies that
if you develop an application by using any language of the .NET family, the code can be easily translated and used by any other .NET language. For example, a Visual C++ .NET application can be easily
converted to a Visual Basic .NET or Visual C# application, and likewise the opposite.
Support for OOPs (object-oriented programming). OOPs is not a new concept for C++ programmers. Code in a OOP-based language is written using classes and objects. This not only helps you to write code easily, but also helps reuse your code. As discussed earlier, .NET has a
6Part I INTRODUCTION TO C#
library of classes that contains methods that you can use to develop your applications. In addition, .NET also supports inheritance, which means that you can derive new classes from existing or base classes, and thus make the base class methods available to the new classes.
Support for Web applications. Creating Web pages using scripting languages such as ASP (Active Server Pages) has not been an easy task for programmers worldwide. Therefore, to make coding simpler for the programmers, .NET provides the ASP.NET technology. The applications that use the ASP.NET technology to create Web pages are called Web applications.
Using ASP.NET, you can create new ASP.NET pages or convert existing ASP pages to ASP.NET pages. ASP.NET also enables you to add highlevel functionality to your Web pages by allowing you to create pages in any of the .NET programming languages. For example, using ASP.NET, you can create dynamic Web pages that allow you to access data from an underlying database.
Support for Web services. .NET helps you to create Web services that you can use to create applications for different platforms that access data through the Internet. To do so, the methods of an instance of a class are called across the Internet and can then be used by applications running on various platforms. In addition, Web services help you to access the functionality of a remote server, such as calling a method from a remote server, creating an instance of a class on a remote server, and performing operations on the remote server. Web services use HTTP, which simplifies your task of accessing a remote server. HTTP helps in transferring messages written using XML between client and server.
As you can see, .NET is set to change the style of programming. The components of .NET that make it a user-friendly development environment are discussed in the following sections.
Common Language Runtime (CLR)
Among the most important components of .NET is the .NET Runtime, commonly called the CLR. As the name suggests, the CLR is a common run-time
OVERVIEW OF THE .NET FRAMEWORK |
Chapter 1 |
7 |
|
|
|
environment for the code written in .NET languages.The code in .NET is managed by the CLR and is, therefore, called managed code. Managed code contains the information about the code, such as the classes, methods, and variables defined in the code. This information contained in managed code is called metadata. The CLR uses metadata to provide safe execution of the program code.
In addition to executing code, the CLR manages memory and threads and helps in the security and interoperability of the code with other languages. Besides providing safe execution of the program, managed code aims at targeting CLR services.These CLR services include locating and loading classes and interoperating with the existing DLL (Dynamic Link Library) code and COM objects.
The CLR has also enabled programmers to achieve interoperability across applications written in any of the .NET languages. Because the CLR is the common runtime environment for all .NET applications, all the code in .NET is converted to MSIL and is executed in a similar fashion. As discussed earlier, this code is called managed code. Managed code in .NET is developed using the CTS or CLS classes. The following section discusses CLS and CTS in detail.
Common Type System (CTS)
As discussed earlier, .NET aims at providing interoperability between applications. To create interoperable applications, you need a set of standard data types that would be used across applications. In addition, you require a set of guidelines to create user-defined classes and objects for the .NET Framework. These standard data types and the set of guidelines are contained in CTS. To ensure interoperability across applications, CTS includes only those data types and features that are compatible across languages.
Consider an example of an application of which a part is created using C++. Subsequently, to provide a visual interface, you need to recreate the entire application in Visual Basic. This means that you need to recreate all the classes that you have used in the C++ application. This is because C++ and Visual Basic are not interoperable. The CTS feature for the .NET Framework simplifies such tedious tasks. If you create a class in any of the languages in the .NET Framework, you can use the same class in another language that is supported by the .NET Framework. Figure 1-1 displays the language interoperability feature of the .NET Framework.
8Part I INTRODUCTION TO C#
Y
L
FIGURE 1-1 Language interoperability featureFof the .NET Framework
By now, you know thatEinteroperabilityMprovided by CTS is a desired feature of all programmers. Following are some of the benefits of the interoperability offered by CTS. T
A
Inherit a class from a class you created in another language.
Create an object of a class created in another language. You can also access the methods of the object that you have created.
Pass an object or a reference of an object as a parameter to the methods of the class that you have written in another language.
Debug an application that contains the objects of classes written in different languages. The debugger of the .NET Framework also enables you to switch between source code of the applications you have written in different .NET languages.
Common Language Specification (CLS)
In addition to CTS, another feature that ensures language interoperability in the
.NET Framework is CLS. CLS is defined as a set of rules that a .NET language should follow to allow you to create applications that are interoperable with other languages. However, to achieve interoperability across languages, you can only use objects with features listed in the CLS. These features are called CLS-compliant features.
For example, C# supports uint32, which is a 32-bit unsigned integer data type that is not CLS-compliant. The data type is not supported by Visual Basic
Team-Fly®
OVERVIEW OF THE .NET FRAMEWORK |
Chapter 1 |
9 |
|
|
|
.NET. If you use the uint32 data type in a C# class, the class might not be interoperable with Visual Basic .NET applications.Only a CLS-compliant code is fully interoperable across languages. Although you can use the non-CLS feature in a
.NET language class, the class might not be available to other .NET languages.
CLS works closely with CTS and MSIL to ensure language interoperability. You can also call CLS as a subset of CTS and MSIL, because CLS does not include all the features of CTS and MSIL. A compiler might not support some of the features of CTS and MSIL and still be CLS-compliant. Following are some of the features of CLS:
Global methods and variables are not allowed in a CLS-compliant language.
Some data types, such as unsigned data types, are not allowed in a CLScompliant language.
Unique names should be used in a CLS-compliant language. Even if the language is case-sensitive, you must use distinct names for different variables. Languages that are case-insensitive should be able to differentiate between the names.
Any exception that you want to handle must be derived from the base
class Exception.
Pointers are not supported by a CLS-compliant language.
Garbage Collector
Consider a situation in which you write extensive code for an application, such as that for an airline reservation system. In the application, you define a large number of variables of different data types, but you do not remove the variables from the system memory. In such a case, variables that are not required occupy memory space, resulting in reduced application performance to the extent that the application might even stop running.
The garbage collection feature of CLR enables automatic management of system memory. If a variable is not referenced for a long period of time by any application, the garbage collector automatically releases the memory assigned to the variable. This memory clean-up process ensures that there is no unnecessary wastage of system memory and, therefore, prevents memory leakage from the application.
In .NET, the CLR handles the process of garbage collection. To understand the garbage collection system, first look at the mechanism of allocating memory to an
