Understanding C Sharp Day 18

Lecture 18 : Understanding C Sharp 

Class Library :

  • A class library defines types and methods that are called by an application.

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

Create New Class Library Project with Visual Studio : 

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 .DDL 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.
Program :



















Compiling Project with Visual Studio 2019 :

Step 1 : Build Your Project, Converting the Source Code into an (.dll file) :
  • Open VS 2019 and click on create new project tab.                                               

  • Select Class Library(.NET Core) and press next button.

  • Give a project name and press create button.

  • Our class library has only one Class1.cs file which contains methods (like multiply) for API.  

  • For converting source code into .dll file build your project. 

  • Then open solution in file explorer to see whether the .dll file is generated or not. 

Step 2 : Using the Class Library in other Visual Studio Projects :
  • Create a new console application on clicking create new tab or you can right click on solution explorer tab and add new item.
  • Select Console Application(.NET Core) and press next button.
  • Give a project name and press create button.
  • Add reference to project solution by right clicking on project and select add->add reference.
  • Class1.cs file contains main method which serve as a entry point of application.
Step 3 : Run Project with 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.

Comments

Popular posts from this blog

Basic Concept In C Sharp Day 16

VS Code In JavaScript Day 6

ASP.NET MVC Interview Questions Part - II Day 26