Flylib.com

Books Software

 
 
 

Introduction to the Base Class Library


Introduction to the Base Class Library

The Base Class Library (BCL), provides a ton of code that you as a developer can leverage to build your applications. The BCL is extensive in terms of the number of classes it provides, and an exhaustive coverage of all that it offers would require a tome all to itself. It is interesting to note that the BCL was developed almost entirely in C#. Table 2.2 provides a basic listing of the FCL namespaces and a partial listing of what can be found under that namespace.

Table 2.2. An FCL Overview

Namespace

Key Classes

System

Basic types ( int , char , string ) Console class for console I/O Standard exceptions

System.Diagnostics

Debugging class Trace listeners Performance counters

System.Drawing

Image support Graphics support for both 2D/3D

System.IO

File I/O Streams

System.Reflection

Metadata classes; used for dynamic discovery of methods , properties, and fields

System.Text

Text manipulation classes StringBuilder

System.Text.RegularExpression

Support for full regular expression

System.Web

Base web core classes HttpHandlers

System.Web.UI

Core web UI classes

System.Web.UI.HtmlControls

Standard HTML control classes

System.Web.UI.WebControls

.NET versions of standard HTML controls, such as button, radio button, table, and so on

System.Windows.Forms

Windows development UI controls


Table 2.2 does not give a complete namespace listing, but instead lists some of the most commonly used namespaces.


The Canonical "Hello World" Example

I'm fairly certain that by law it is required for every programming book to provide the famous "Hello World" example. So, rather than face potential criminal charges, a walkthrough for creating a Console C# "Hello World" application follows .

Begin by staring Visual Studio .NET and selecting File, New Project. Next, select a C# console application and name it Hello World as shown in Figure 2.3.

Figure 2.3. Creating a .NET console application for Hello World.


Visual Studio .NET will create a default source file, Class1.cs , and provide some basic boilerplate code as shown in Listing 2.4.

Listing 2.4. Generated Boilerplate Code for a C# Console Application
using System;

   namespace Hello_World
   {
       /// <summary>
       /// Hello World example
       /// </summary>
       class Class1
       {
           /// <summary>
           /// The main entry point for the application.
           /// </summary>
           [STAThread]
           static void Main(string[] args)
           {



//




// TODO: Add code to start application here




//


}
       }
   }

Replace lines 16 though 18 with the following line of code:

Console.WriteLine( "Hello World" );

The Main method should now look like Listing 2.5.

Listing 2.5. Updated Main Method Replacing Comments with Code
1: static void Main(string[] args)
2: {
3:     Console.WriteLine( "Hello World" );
4: }

The Console.WriteLine method will print the string Hello World to a console window. Figure 2.3 shows the results from executing the code. To execute the code, select Debug, Start Without Debugging or simply press Ctrl+F5 to launch the application.