0% found this document useful (0 votes)
36 views

Tutorial - Class Library .DLL For C# - Codementor

This tutorial shows how to create a class library DLL in C# that can be used by other projects. It involves installing Visual Studio, creating a class library project, adding a class with math methods like addition, then compiling the project to generate the DLL file. This DLL can then be added as a reference to other projects, like a console app, and the math methods can be called to reuse the shared code. The tutorial provides example code and steps to set up the class library and demonstrate its use in another project.

Uploaded by

ed judge
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

Tutorial - Class Library .DLL For C# - Codementor

This tutorial shows how to create a class library DLL in C# that can be used by other projects. It involves installing Visual Studio, creating a class library project, adding a class with math methods like addition, then compiling the project to generate the DLL file. This DLL can then be added as a reference to other projects, like a console app, and the math methods can be called to reuse the shared code. The tutorial provides example code and steps to set up the class library and demonstrate its use in another project.

Uploaded by

ed judge
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

5/14/2020 Tutorial - Class Library .

DLL for C# | Codementor

Want to learn programming but don’t know where to start? Check 


out WRITE
our free Coding
A POST
Starter Kit to get started! 

Thomas Marco de Haan FOLLOW


Senior Software Developer - C#, Java, Python, C\C++, Javascript, PHP

Tutorial - Class Library .DLL for C#


Published Jan 02, 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.

By using Codementor, you agree to our Cookie Policy. ACCEPT

Enjoy this post? 2


Install Visual Studio IDE
https://www.codementor.io/@dewetvanthomas/tutorial-class-library-dll-for-c-129spithmr 1/17
5/14/2020 Tutorial - Class Library .DLL for C# | Codementor

Step 1 : Download Visual Studio Installer


 WRITE A POST
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

By using Codementor, you agree to our Cookie Policy. ACCEPT

Enjoy this post? 2

https://www.codementor.io/@dewetvanthomas/tutorial-class-library-dll-for-c-129spithmr 2/17
5/14/2020 Tutorial - Class Library .DLL for C# | Codementor

 WRITE A POST

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

By using Codementor, you agree to our Cookie Policy. ACCEPT

Enjoy this post? 2

https://www.codementor.io/@dewetvanthomas/tutorial-class-library-dll-for-c-129spithmr 3/17
5/14/2020 Tutorial - Class Library .DLL for C# | Codementor

 WRITE A POST

By using Codementor, you agree to our Cookie Policy. ACCEPT

Enjoy this post? 2

https://www.codementor.io/@dewetvanthomas/tutorial-class-library-dll-for-c-129spithmr 4/17
5/14/2020 Tutorial - Class Library .DLL for C# | Codementor

 WRITE A POST

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
By using Codementor, you agree to our Cookie Policy.
Multiply numbers ACCEPT

Enjoy this post? 2

https://www.codementor.io/@dewetvanthomas/tutorial-class-library-dll-for-c-129spithmr 5/17
5/14/2020 Tutorial - Class Library .DLL for C# | Codementor

 WRITE A POST

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

By using Codementor, you agree to our Cookie Policy. ACCEPT

Enjoy this post? 2

https://www.codementor.io/@dewetvanthomas/tutorial-class-library-dll-for-c-129spithmr 6/17
5/14/2020 Tutorial - Class Library .DLL for C# | Codementor

using System;  WRITE A POST


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

By using Codementor, you agree to our Cookie Policy. ACCEPT

Enjoy this post? 2

https://www.codementor.io/@dewetvanthomas/tutorial-class-library-dll-for-c-129spithmr 7/17
5/14/2020 Tutorial - Class Library .DLL for C# | Codementor

 WRITE A POST

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

By using Codementor, you agree to our Cookie Policy. ACCEPT

Enjoy this post? 2

https://www.codementor.io/@dewetvanthomas/tutorial-class-library-dll-for-c-129spithmr 8/17
5/14/2020 Tutorial - Class Library .DLL for C# | Codementor

 WRITE A POST

By using Codementor, you agree to our Cookie Policy. ACCEPT

Enjoy this post? 2

https://www.codementor.io/@dewetvanthomas/tutorial-class-library-dll-for-c-129spithmr 9/17
5/14/2020 Tutorial - Class Library .DLL for C# | Codementor

 WRITE A POST

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.
By using Codementor, you agree to our Cookie Policy. ACCEPT

Adding MathLibrary.dll
Enjoy this post? as a Reference 2

https://www.codementor.io/@dewetvanthomas/tutorial-class-library-dll-for-c-129spithmr 10/17
5/14/2020 Tutorial - Class Library .DLL for C# | Codementor

Step 1: Add the following References to your Project Solution


 WRITE A POST
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.

By using Codementor, you agree to our Cookie Policy. ACCEPT

Enjoy this post? 2

https://www.codementor.io/@dewetvanthomas/tutorial-class-library-dll-for-c-129spithmr 11/17
5/14/2020 Tutorial - Class Library .DLL for C# | Codementor

 WRITE A POST

By using Codementor, you agree to our Cookie Policy. ACCEPT

Enjoy this post? 2

https://www.codementor.io/@dewetvanthomas/tutorial-class-library-dll-for-c-129spithmr 12/17
5/14/2020 Tutorial - Class Library .DLL for C# | Codementor

 WRITE A POST

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.

By using Codementor, you agree to our Cookie Policy. ACCEPT

Enjoy this post? 2

https://www.codementor.io/@dewetvanthomas/tutorial-class-library-dll-for-c-129spithmr 13/17
5/14/2020 Tutorial - Class Library .DLL for C# | Codementor

using System;  WRITE A POST


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 MathLib

// 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.
By using Codementor, you agree to our Cookie Policy. ACCEPT
To run the code with hotkeys, Press Ctrl + F5 to run your project
Enjoy this post? 2

https://www.codementor.io/@dewetvanthomas/tutorial-class-library-dll-for-c-129spithmr 14/17
5/14/2020 Tutorial - Class Library .DLL for C# | Codementor

Result MathLibrary.dll & Console App Project


 WRITE A POST
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

C# Class library Dlls Tutorial C# tutorial


By using Codementor, you agree to our Cookie Policy. ACCEPT

Enjoy this post? 2

https://www.codementor.io/@dewetvanthomas/tutorial-class-library-dll-for-c-129spithmr 15/17
5/14/2020 Tutorial - Class Library .DLL for C# | Codementor

 WRITE A POST
Enjoy this post? Give Thomas Marco de Haan a like if it's helpful.

2  SHARE

Thomas Marco de Haan


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...

FOLLOW

Be the first to share your opinion

Leave a reply

Find a Pair Programming


Partner on Codementor
Want to improve your programming skills? Choose from 10,000+
mentors to pair program with.
By using Codementor, you agree to our Cookie Policy. ACCEPT

Enjoy this post? 2

https://www.codementor.io/@dewetvanthomas/tutorial-class-library-dll-for-c-129spithmr 16/17
5/14/2020 Tutorial - Class Library .DLL for C# | Codementor

GET STARTED  WRITE A POST

Harris Robin Kalash

Getting Started with the New React Navigation v5

By now, we've all heard about React Navigation V5. It comes with a bunch of
improvements and a completely new approach to configuration that is component
based vs API based. You can learn more about this new version here.

React Navigation v5 is a big release that completely changes how your navigation is
configured. That being said, it is understandable that with such a release, upgrading
and understanding everything going on can be difficult. This is why boilerplates like
Ignite Bowser are g ... READ MORE

By using Codementor, you agree to our Cookie Policy. ACCEPT

Enjoy this post? 2

https://www.codementor.io/@dewetvanthomas/tutorial-class-library-dll-for-c-129spithmr 17/17

You might also like