A library, in the real world, is a place that contains a large number of books and other sources of information. When people need to know something, they don't have to go and discover it all over again and write their own book - they can simply refer to the correct books, saving a lot of time and effort.
In the same way, people all across the world have written huge libraries of C# code. So, instead of us writing everything ourselves, it often makes sense to use someone else's libraries for parts of our programs.
This still takes some effort - you need to look up what's in the library and understand which parts are useful to you - but it allows you to concentrate more on doing what your program needs to do, rather than wasting a lot of time writing the background code.
By far the most important library for C# is Microsoft's ".NET Framework Class Library". You can't really write any useful program that doesn't use something from it. Part III of this book is all about that library. When you install the .NET Framework on your computer, the library is automatically loaded and is therefore available to your programs. You just need to know how to refer to it - how to use it from your programs.
An Example
The "System.Drawing" part of the .NET Framework Class Library contains many useful classes for working with pictures. Let's use it to illustrate how to use other people's code in your own program.
The System.Drawing.Image class has a method called RotateFlip, which will rotate (turn around) or flip (reverse) any picture that you pass to it. Suppose you want to use that method in your own program - you want to load a picture file from disk, flip it horizontally, and save a copy of the flipped picture back to disk. Here's one way to do so. This is a complete, working program. (If you're experimenting, note that program 9 in the collection of example files provided with this book, is a similar program).
| class PhotoSizer
{
public PhotoSizer()
{
// Load a photograph from disk into the computer's memory
System.Drawing.Image img;
img = new System.Drawing.Bitmap(@"d:\samples\myDog.jpg");
// Flip the image horizontally
img.RotateFlip( System.Drawing.RotateFlipType.RotateNoneFlipX );
// Save the flipped photo to a different disk file
img.Save( @"d:\samples\myDogFlipped.jpg" );
}
static void Main()
{
PhotoSizer p = new PhotoSizer();
}
} |
A quick note about the @ symbol :
The C# @ symbol used above can be added at the beginning of a string to “escape” or ignore backslash characters if the path includes special characters like backslashes. Backslashes can confuse C# as they have special meaning. So usually you would have to write such a path with double backslashes, such as d:\\samples\\myDog.jpg. The @ symbol avoids the need to do that.)
What we've done in the example above is to use a class from a library. The class is called Image and it is in the section of the library called System.Drawing.
After creating an Image object, we called two methods of the image class. The methods' names are RotateFlip and Save.
An Easier Way
But you can see that each time you need to talk about something in the System.Drawing space, you need to write those long words again. This is a waste of energy and there is a way to avoid it. Instead, you can tell your program once at the top that you'll be "using" some things from the System.Drawing namespace as well as some things from the System.Drawing.Imaging namespace specifically.
using System.Drawing; using System.Drawing.Imaging;
class PhotoSizer
{
public PhotoSizer()
{
// Load a photograph from disk into the computer's memory
Image img = new Bitmap(@"d:\samples\myDog.jpg");
// Flip the image horizontally
img.RotateFlip( RotateFlipType.RotateNoneFlipX );
// Save the flipped photo to a different disk file
img.Save( @" d:\samples\myDogFlipped.jpg" );
}
static void Main()
{
PhotoSizer p = new PhotoSizer();
}
} |
A Note About References in C# Express
When creating a Visual C# Express project, it is also necessary to create something called a "reference" pointing to the Class Library file which holds the library code you want to use (these files have a “.dll” extension that stands for “Dynamic Link Library"). Fortunately, the most common references are usually added for you automatically.
The picture below, for example, shows the references that are added when you create a “Windows Application” project. Visual C# Express guesses the parts of the library that it thinks you might use for an application like that.
In the example above, you can see that the classes of the System.Drawing namespace are held in a file called System.Drawing.dll.
If the part of the library you want to use has not been automatically referenced for you, you need to add a reference yourself. Suppose, for example, you find a code example that uses the System.Web.HttpRequest class to retrieve some information from a web server, and you want to try it. You would right-click on “References”, select “Add Reference” and select the System.Web part of the library. Now you will find you can use its classes in your code.
Conclusion
So it's quite simple to include code from a library. The hard part is knowing what classes, methods, etc. are available for you to use in this vast library.
That's why part IV of this book has been written - to expose you to some of the useful parts of Microsoft's .NET Framework class library.