Project Breakdown – Class Libraries

In all of our posts to date, we have been working with a single project in a solution. That project type was a Console Application. However, there are several other types of projects you can create with Visual Studio. This post will discuss the Class Library project, explaining what it is, how to create one, and how to reference it in another project.



What is A Class Library

Sometimes you will create classes and methods that would beneficial to use in many applications. Instead of duplicating the functionality in every application you develop, it makes more sense to store it the code in a container that can be used over and again, by any application. A class library is a perfect way to accomplish this.


You can add a class library project to the same solution as your application. This process allows you to test the functionality of the code while retaining the ability to update the library. The downside to this is that you would have to add the project to every solution that wants to use the code. If you want to use your library in multiple applications, you need to compile the library into a file that can be referenced by any project.


When you build your class library project, a DLL file is created. DLL stands for Dynamic Link Library, and it is a file that stores classes and methods that can be referenced in any project. The DLL files you reference in your project don’t have to be created by you. Many applications reference DLLs created by someone else to add functionality to their applications.

Creating A Class Library

You create a class library just as would a Console Application, only this time you select “Class Library.” You should name your project, something that implies that it is a library of classes and methods to be used by other applications (e.g., MathFunctions, ConsoleFormatLibrary).

When Visual Studio creates the class library project for you, it creates a single class named “Class1”. The first thing you should do is change the name of the class to one that makes sense.

namespace ConsoleFormatLibrary
{
 	public class CFL
 	{
 	}
}

Take notice of the NAMESPACE keyword. I haven’t discussed namespace in depth before, but understanding them is essential when working with class libraries. Their importance does not come into play until you are referencing a class library. Because of that, I will go into greater detail about namespaces later in this post. For now, just pay attention to the name of the namespace.


Also, take notice of the access modifier of the class. When you create a class library, the class is automatically set to PUBLIC. Since class libraries are meant to be used by many applications, their classes should be PUBLIC.


As for the fields, properties, and methods of the class library, you should follow the same process and suggestions you read about in our Creating Objects with Classes post.

When you are done writing the code for your library, you can build the project, and the DLL file will be created.

Adding a Class Library

There are two ways you can utilize a class library in your existing project. You can add the class library project to your solution, or you can add a reference to a DLL file.

Class Library Project

If you want to use a Class Library project in you solution you can create a new one or add an existing one into your solution.

Once this is complete, you will see the class library project in your Solution Explorer.


Dynamic Link Library

Adding a class library project to your solution makes sense, if there is still work do be done on that library, or to test out its functionality. However, to utilize a completed library, the best practice is to add a reference to the DLL file. Also, there will be times when you don’t have access to the class library project, only the DLL file.


Regardless of which option you choose, there are two steps you will have to take to utilize the classes and methods in the library.


Add a Reference

For your project to see the class library, it needs to know it exists. You can accomplish this by adding a reference to the project. To add a reference to your project, right-click on the “References” in the Solution Explorer, and select “Add Reference.”

When you do that, the Reference Manager window will display.


This window will allow you to add a reference to a project:


Or you can add a reference to a Dynamic Link Library. You will have to use the “browse” button to find any DLL files that you have built.


When you add a reference to a DLL file, you will see the namespace of the library under the References hierarchy.


Using A Class Library

Once you have a reference to the class library, you can use the classes and method of the library in your code.

 static void Main(string[] args)
 {
 	ConsoleFormatLibrary.CFL.DisplayRedText("Hello There!");
 	Console.ReadLine();
 }

As you can see in the code example, I can use the DisplayRedText method from the CFL class. However, to use the method right now, I have to tell Visual Studio where to find the CFL class by using the namespace of the CFL class.


Namespaces

A namespace is precisely that, a space with a name that Visual Studio uses to look for classes.


To use a real-world analogy, think of a person as a class. If you want to know about that person's attributes or ask them to do something for you, you need to know how to reach them. The namespace would resemble their address. It would provide you with their location, and so you could find them and request something from them. C# has so many libraries already created for you that if they weren’t in their namespace, there would be no way to determine which class you are trying to use.

Using the Namespace

To shortcut the need to call out the namespace every time you want to use the classes and methods of the class library, you can add the namespace as a location Visual Studio will store in its collection of places to look for code. You do this with the USING keyword. The using clause informs Visual Studio that you will be using code from any of the namespaces called out with the USING keyword.

using ConsoleFormatLibrary;

58 views0 comments

Recent Posts

See All