Building class libraries

Class library assemblies group types together into easily deployable units (DLL files). So far, you have only created console applications to contain all your code. To make the code that you write reusable across multiple projects, you should put it in class library assemblies, just like Microsoft does.

Tip

Put types that you might reuse in a class library.

Creating a class library to share code

Start Microsoft Visual Studio 2015. In Visual Studio, press Ctrl + Shift + N or go to File | New | Project….

In the New Project dialog, in the Installed Templates list, select Visual C#. In the center list, select Class Library, type Name as Ch06_PacktLibrary, change Location to C:\Code, type Solution name as Chapter06, and then click on OK.

Note

Make sure you choose class library and not a console application!

Defining a class

In Solution Explorer, right-click on the file named Class1.cs and choose Rename. Type the name as Person. When you are prompted to rename all other references to the class, click on Yes:

Change the namespace to Packt.CS6 because it is important to put your classes in a logically named namespace. Your code should now look like this:

namespace Packt.CS6
{
    public class Person
    {
    }
}

Notice that I chose to apply the C# keyword public before the class. This allows all code to access this class. If you do not explicitly apply the public keyword, then it would only be accessible within the assembly that defined it. We need it to be accessible outside the assembly too. This type does not yet have any members encapsulated within it. We will create some soon.

Members can be fields, methods, or specialized versions of both. They are described here:

  • Fields are used to store data. These are the three specialized fields:
    • Constants: The data in this field never changes
    • Read-only fields: The data in this field cannot change after the class is instantiated
    • Events: These point to methods that you want to call automatically when something happens, such as clicking on a button
  • Methods are used to execute statements. These are the four specialized methods:
    • Constructors: These are a type of method that execute when you use the new keyword to allocate memory and instantiate a class
    • Properties: These are a type of method that execute when you want to control access to fields
    • Indexers: These are a type of method that execute when you want to control access to fields
    • Operators: These are a type of method that execute when you want to apply an operator

Instantiating a class

In this section, we will make an instance of the Person class.

Add a new console application project named Ch06_PeopleApp.

Note

Make sure you choose console application and not a class library!

This project needs a reference to the class library we just made.

In Solution Explorer, right-click on References and choose Add Reference…:

In the Reference Manager dialog box, in the list on the left-hand side, choose Projects, select the Ch06_PacktLibrary assembly, and then click on OK:

At the top of the file, type the following code to import the namespace for our class and to statically import the Console type:

using Packt.CS6;
using static System.Console;

In the Main method, type the following code to create an instance of the Person type by using the new keyword. The new keyword allocates memory for the object and initializes any internal data. We could use Person in place of the var keyword, but the use of var involves less typing and is just as clear:

var p1 = new Person();
WriteLine(p1.ToString());

Press Ctrl + F5. If you see the following dialog box, then dismiss it:

Set the solution's startup project to the current selection. Click inside the Ch06_PeopleApp project or inside the Program.cs file, and press Ctrl + F5 again.

Although our Person class did not explicitly choose to inherit from a type, all types indirectly inherit from a special type named System.Object. The implementation of the ToString method in the System.Object type simply outputs the full namespace and type name like this:

Packt.CS6.Person

Back in the original Person class, we could have explicitly told the compiler that Person inherits from the System.Object type like this:

public class Person : System.Object
Tip

When class A inherits from class B, we say that B is the base or super class and A is the derived or subclass. In this case, System.Object is the base or super class and Person is the derived or subclass.

You can also use the C# alias object keyword:

public class Person : object

Modify the code to explicitly inherit from the object type. Then, click inside the keyword and press F12. You will see the Microsoft-defined System.Object type and its members. You do not need to understand any of this yet, but notice that it has a method named ToString, as you can see in the following screenshot:

Tip

Assume other programmers know that if inheritance is not specified, the class will inherit from System.Object.