3. Introducing the C# Class Library

Believe it or not, the string conversion methods discussed in the previous section are not technically necessary. With a little effort, you could write a list of C# statements to do the conversions between numeric and string types. Don't believe me? Take a look at this code:

int num = ... some calculation that produces a positive int ...;

// Now, let's convert it to a string...
string result = "";
if (num == 0) {
  result = "0";
} else {
  while (num > 0) {
    int digit = num - ((num / 10) * 10);
    result = digit + result;
    num = num / 10;
  }
}

This code will convert any positive integer into a string, stored in the variable result. It doesn't handle negative numbers, but it wouldn't be difficult to add support for those.

Thankfully, someone has already taken the trouble to write conversion code for you, and to package it into a predefined method that is part of the standard C# Class Library. When I write a C# program, I could "reinvent the wheel" and write the individual steps to convert a numeric value to a string, but that would be wasted effort. Instead, I use the existing work by calling the methods to do the work for me.

The C# Class Library (also known as the C# API, or Application Programming Interface) is a collection of predefined classes that contain methods that do all kinds of useful things. Being an effective C# programmer means more than just knowing the language and how to write code; it means knowing the methods in the Class Library and how and when to use them to solve a certain problem (such as converting an int to a string), rather than writing the code yourself. The rest of this chapter introduces the library and helps you to use it.

3.1. Class Library Overview

The C# Class Library contains thousands of classes. As the last chapter discussed, the classes are organized in groups called namespaces. The System namespace contains core classes that many programs need to use. Here are some important classes in the System namespace:

  • Console - Contains methods to perform console input/output

  • Convert - Contains methods to convert between strings and other data types

  • Environment - Contains methods to access information about the computer configuration

  • Math - Contains methods to perform mathematical calculations

  • String - Contains methods to process string data

You have seen the Console and Convert classes used extensively in programs. The rest of this chapter features the Math and String classes.

Later in this book, you will also use classes in these namespaces:

  • System.IO - Contains classes to access files

  • System.Web - Contains classes used in web applications

  • System.Collections - Contains classes used to hold collections of data

3.2. Introducing the Math Class

Earlier in this book, you learned that a class is a container for a group of methods. The Math class contains several methods that help you do numerical processing. Here are some of the methods available:

  • Sin() - computes the sine of an angle

  • Sqrt() - computes the square root of a number

  • Log() - computes the logarithm of a number

  • Pow() - raises a number to a power

  • Round() - rounds a fractional number to the nearest integer

In the next section, you will learn how to use some of these methods.

In addition to methods, classes sometimes contain predefined constants - data values you can use in your program. The Math class contains two constants:

  • PI - an approximation of the value of pi

  • E - an approximation of the value of the natural logarithmic base

To use a constant in your program, you write the name of the class, followed by a dot, followed by the name of the constant. For example, to use the PI constant in a calculation, you might write code like this:

double diameter = 50.35;
double circumference = Math.PI * diameter;