Introduction to Custom Libraries in C# |
|
Introduction to Custom Libraries |
Introduction |
The .NET Framework is a huge library made of various classes aimed at different scenarios. We will explore some of these classes in our lessons. Still, if the .NET Framework does not provide a functionality you are looking for, you can create you own library and use it in one or more programs. |
You can even create a commercial library and distribute or sell it. For example, 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() function. A library usually has the extension .dll (other types of libraries in Microsoft Windows can have the extension .lib or another extension). |
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 middle 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:
In both cases, a skeleton code would be presented to you and you can complete it as you see fit. Here is an example: public class Algebra { public double Addition(double x = 0, double y = 0) { return x + y; } public double Subtraction(double x = 0, double y = 0) { return x - y; } public double Multiplication(double x = 0, double y = 0) { return x * y; } public double Division(double x = 0, double y = 1) { return x / y; } }
Since you would be creating a library and not an executable, to compile the project:
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:
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:
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;
public class Exercise
{
static void Main()
{
Algebra alg = new Algebra();
double number1 = 244.58;
double number2 = 5082.88;
double result = alg.Addition(number1, number2);
Console.Write(number1);
Console.Write(" + ");
Console.Write(number2);
Console.Write(" = ");
Console.WriteLine(result);
}
}
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 © 2010-2016, FunctionX | |
|