Home

Creating a Custom Library

 

Introduction

 

If the .NET Framework does not have a class you are looking for, you can create one and be able to use it over and over again in different programs. You can even create a commercial class and be able to distribute or sell it. To make this possible, you can "package" one or more classes in a library.

A library is a program that contains classes and/or other resources that other programs can use. Such a program is created with the same approach as the programs we have done so far. Because a library is not an executable, it does not need the Main() method. A library usually has the extension .dll (other types of libraries in Microsoft Windows can have the extension .lib or others).

 

Creating a Library

A library can be made of a single file or as many files as necessary. A file that is part of a library can contain one or more classes. Each class should implement a behavior that can eventually be useful and accessible to other classes. The classes in a library are created exactly like those of other programs.

To create a library, on the main menu of Microsoft Visual Studio, you can click File -> New -> Project... In the Templates list, you can click Empty Project or click Class Library. Then give it a name:

 If you had selected Empty Project in the New Project dialog box, you should open the Properties window and, in the Output Type combo box, select Class Library:

Properties

In both cases, a skeleton code would be presented to you and you can complete it as you see fit. Here is an example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Calculations
{
    public class Algebra
    {
        public static double Addition(double x, double y)
        {
            return x + y;
        }

        public static double Subtraction(double x, double y)
        {
            return x - y;
        }

        public static double Multiplication(double x, double y)
        {
            return x * y;
        }

        public static double Division(double x, double y)
        {
            if (y == 0)
                return 0;
            return x / y;
        }
    }
}

Since you would be creating a library and not an executable, to compile the project:

  • On the main menu, you can click Build -> Build ProjectName
  • In the Solution Explorer, you can right-click the name of the project and click Build
  • In the Class View, you can right-click the name of the project and click Build

If you want to compile a library at the Command Prompt, you would type:

csc /target:library NameOfFile.cs

and press Enter.

After building the project, you can use it. You can use it in the same project where the library was built or you can use it in another project. If you are working in Microsoft Visual Studio, you can start by creating a new project. To use the library, you would have to reference the library. To do this:

  • On the main menu, you would click Project -> Add Reference...
  • In the Solution Explorer, you would right-click References and click Add Reference...

In both cases, the Add Reference dialog box would come up. You can click the Browse tab, locate the folder where the library resides and select it:

Add Reference

After selecting the library, you can click OK. You can then use the classes and methods of the library like you would use those of the .NET Framework. Here is an example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Algebra1
{
    public class Program
    {
        static int Main(string[] args)
        {
            double Number1 = 244.58;
            double Number2 = 5082.88;
            double Result =
                Calculations.Algebra.Addition(Number1, Number2);

            Console.WriteLine("{0} + {1} = {2}\n",
                Number1, Number2, Result);
            return 0;
        }
    }
}

Or

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Calculations;

namespace Algebra1
{
    public class Program
    {
        static int Main(string[] args)
        {
            double Number1 = 244.58;
            double Number2 = 5082.88;
            double Result = Algebra.Addition(Number1, Number2);

            Console.WriteLine("{0} + {1} = {2}\n",
                Number1, Number2, Result);
            return 0;
        }
    }
}

If you want to compile the project at the Command Prompt, you would type something like the following:

csc /target:library /out:DesiredNameOfLibrary.dll NameOfFile.cs

This would produce:

244.58 + 5082.88 = 5327.46

Press any key to continue . . .

 

 

Home Copyright 2008 Yevol