How To Create Class Library In Visual Studio 2019
Tutorial - Class Library .DLL for C#
Published Jan 02, 2020 Last updated Jan 03, 2020
This tutorial shows, how to build a simple Class Library .DLL in the C# Programming Language. The Class Library .DLL contains program code, data, and resources that can be can used by other programs and are easily implemented into other Visual Studio projects.
Install Visual Studio IDE
Step 1 : Download Visual Studio Installer
- Visit visualstudio.microsoft.com and navigate to the Windows Download Page
- Select either the Proffessional, Community or Enterprise Edition and press the download button.
- If you're an individual programmer or a small team, then i would recommend, downloading the free community edition
Step 2 : Run The Visual Studio Installer
- When you have downloaded an installer of your choice, run it by double clicking the downloaded file. An installer dialog will appear on your screen.
- In the Workloads Tab view, Select .NET Desktop Development if you want to develop for the Windows Desktop
Create New Class Library Project With Visual Studio
Step 1 : Create New Project
- Select C# in the language tab
- And then select Class Library (.NET Framework) and press the NEXT button
- You can then proceed to give your project a Project Name
- Call the project MathLibrary (in order to copy/paste example code)
- Press the CREATE button, After you have given a name to your project
Building the Class Library .DLL for C#
The following code example is a C# version of the Class Library. The Class Library .DLL contains program code, data, and resources. The Class Library in this example will feature a custom Math API that has a set of functions to Substract, Devide and Multiply numbers
Project File Structure
Our Class Library project contains only of 1 file. The Class1.cs file which is our API containing the methods to calculate simple mathematical operations.
Class1.cs (Example Code)
The Class1.cs file is a C# class from our MathLibrary.dll that contains our methods to Substract, Multiply, Devide numbers.
- Substract() Method for subtracting 2 numbers.
- Multiply() Method for multiplying 2 numbers.
- Devide() Method for deviding 2 numbers.
- Power() Method for calculating the power a number
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace MathLibrary { public class Class1 { public float Substract( float a, float b) { return a - b; } public float Multiply( float a, float b) { return a * b; } public float Devide( float a, float b) { return a / b; } public floa Power( float a) { return a * a; } } } Compiling Project with Visual Studio 2019
Step 1: build your project, converting the source code into an (.dll file)
- To build your project, choose Build Solution from the Build menu.
- The MathLibrary.dll is now created in the \bin\Debug folder of your Project
PART 2 : Using the Class Library in other Visual Studio Projects
Step 1 : Create New Console Application Project With Visual Studio
- Select C# in the language tab
- And then select Console App (.NET Framework) and press the NEXT button
- You can then proceed to give your project a Project Name
- Call the project MathApplication (in order to copy/paste example code)
- Press the CREATE button, After you have given a name to your project
Console App Project File Structure
Our Console App project is relatively small and will consist of 2 files. The Program.cs which is the entry point of our application, and the MathLibrary.dll file which contains our API containing the methods to devide, multiply, substract numbers.
Adding MathLibrary.dll as a Reference
Step 1: Add the following References to your Project Solution
- Add MathLibrary.dll to Project References
NOTE: to Add Custom References, click the References tab. Click the Add References button to open the Add Reference dialog box. In the Add Reference dialog box, select the Browse tab indicating the type of component you want to reference. Select the components you want to reference, and then click OK.
Program.cs (Example Code)
The Program.cs file is a C# class that contains the Main method, which serves as the entry point of our Console Application. Usually In computer programming, an entry point is where the first instructions of a program are executed. Here we will manage the Console UI, use functions from the MathLibrary.dll and display it's results on screen.
using System; using MathLibrary; // Importing Math Library namespace MathApplication { class Program { static void Main( string[] args) { // Declare class from MathLibrary.dll Class1 math = new Class1(); // Declare variables and use methods from MathLibrary float substract = math.Substract(5, 2); float multiply = math.Multiply(5, 2); float devide = math.Devide(5, 2); float power = math.Power(5); Console.WriteLine("This Application uses functions from MathLibrary.dll todo simple calculations"); // Print results on screen Console.WriteLine(substract); Console.WriteLine(multiply); Console.WriteLine(devide); Console.WriteLine(power); Console.ReadLine(); } } } Run Project with Visual Studio 2019
Visual Studio builds your project, converting the source code into an executable (.exe file)
- To build your project, choose Build Solution from the Build menu.
- To run the code, on the menu bar, choose Debug > Start Debugging. A console window opens and then runs your app.
- To run the code with hotkeys, Press Ctrl + F5 to run your project
Result MathLibrary.dll & Console App Project
- If everything went right the application should display the numbers calculated by the MathLibrary.dll
- You can download the C# Source Code for the MathLibrary.dll & Console App Project for testing and editing Download Source Code
Enjoy this post? Give Thomas Marco de Haan a like if it's helpful.
Senior Software Developer - C#, Java, Python, C\C++, Javascript, PHP
Jedi Master with 14+ years of experience in software and web development, and author of some popular Windows Tools. Currently working as software engineer for the healthcare industry. I also run an independent software company, f...
Discover and read more posts from Thomas Marco de Haan
Enjoy this post?
Leave a like and comment for Thomas
How To Create Class Library In Visual Studio 2019
Source: https://www.codementor.io/@dewetvanthomas/tutorial-class-library-dll-for-c-129spithmr
Posted by: laliberteflooke42.blogspot.com

0 Response to "How To Create Class Library In Visual Studio 2019"
Post a Comment