Home

C# Libraries: Introduction

 

Introduction

If the .NET Framework doesn't 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 doesn't need the Main() function. A library usually has the extension .dll.

 

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 we have used so far. Everything depends on how you compile it.

To create a library, start by typing its code in a text file. Once the library is ready, to compile it, at the Command Prompt, you would type

csc /target:library NameOfFile.cs

and press Enter. After doing this, a library with the name of the file and the extension .dll would be created. If you want a custom name, use the following syntax:

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

Practical LearningPractical Learning: Creating a Library

  1. To start a new project, on the main menu, click File -> New Project...
  2. In the New Project dialog box, click Class Library
  3. Set the Name to Operations1 and click OK
  4. Change the file as follows:
     
    using System;
    
    namespace Operations1
    {
        public class Operations
        {
            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;
            }
        }
    }
  5. In the Solution Explorer, right-click Class1.cs and click Rename
  6. Change the name to Operations.cs and press Enter
  7. To save the project, on the Standard toolbar, click the Save All button
  8. Click Save to save everything
  9. On the main menu, click Project -> Operations1 Properties
  10. In the Output Type combo box, make sure Class Library is selected:
     
  11. Click the X button to close the Properties window
  12. To create the library, on the main menu, click Build -> Build Solution
  13. To start another project, on the main menu, click File -> New Project...
  14. In the New Project dialog box, select Console Application
  15. Set the Name to Algebra1 and press Enter
  16. In the Solution Explorer, right-click References and click Add Reference...
  17. Click the Browse tab
  18. In the list of folders, double-click Operations1 and locate the Operations1.dll file (it should be in the Release (or the Debug) sub-folder of the bin folder)
  19. Click Operations1.dll
     
  20. Click OK.
    In the Solution Explorer, expand the References node if necessary and make sure that there is a new node labeled Operations1
  21. Access the Program.cs file and change it as follows:
     
    using System;
    
    namespace Algebra1
    {
        class Program
        {
            static int Main()
            {
                double Number1 = 244.58;
                double Number2 = 5082.88;
                double Result  = 
                    Operations1.Operations.Addition(Number1, Number2);
    
                Console.WriteLine("{0} + {1} = {2}\n",
                    Number1, Number2, Result);
                return 0;
            }
        }
    }
  22. Execute the application to test it. This would produce:
     
    244.58 + 5082.88 = 5327.46
    
    Press any key to continue . . .
  23. Close the DOS window
 

Home Copyright © 2006-2016, FunctionX, Inc.