What is a C# Library and How do I Build One from Scratch

By i_Zesty

Introduction

In the area of coding and development, the terms “Library” and “API” often go hand in hand, for the sake of this post, we’ll use “API” to refer to what is commonly known as a “Library”.

API’s (Application Programming Interfaces) are invalueable tools in the world of application and game development. This post will serve as a gateway to understanding the basics of a C# API and allow you to create your own and intergrate them into your own projects!

In this post, we will also be using Microsoft Visual Studio to write and play around with our API.

What is an API exactly?

At its core, an API; which stands for “Application Programming Interface”, is (at a base); somewhat like a “communication system” for apps and games, when developing software it is very useful to use API’s to help with tasks such as

  • Executing functions in different files or programs.
  • Moving data.
  • Communicating between in real time between different applications
  • And much more!

Imagine you have 2 apps that you would like them to seamlessly share and exchange files between one-another, rather than writing a system to save and load a file in each application, you can opt for a more efficient solution - an API. By implementing an API you can create a more streamlined connection between both apps, allowing them to effortlessly save and load files without the need of redundant and repeated code between each program!

Setting up your API project

For our example, we will be making a simple math API - this could be used for something such as a calculator or just general use!

First of all, you’re going to want to create a new C# “Class library” project as seen in the image below. Setting up our project

Name your project whatever you like, and then set the Target Framework to “.NET 5.0” (as this is the framework we will be using to develop our API)

Now that you have created your project, you should be seen with a window similar to below! Project landing page

Creating an API method

First of all, we are going to want to rename our class file to keep our project organised, for this example - we will simply name it “Calculate.cs” Class file rename

In our code-space, we will start off by creating our “Addition” method.

Creating our API methods

Within our public class Calculate, we are going to want to write a public int method - this will return us an int when we call it.

public class Calculate
{
    public int Add(int num1, int num2)
    {

    }
}

Now that we have our base, we will see that our method has an error. Int method error This is due to us not returning a valid value, as our method type is an int, we are going to have to return an int value.

So to continue on with this, we will finish off our method with returning our sum as an int value by simply returning num1 + num2

public class Calculate
{
    public int Add(int num1, int num2)
    {
        return (num1 + num2);
    }
}

And there we have it, our first API method!


Just for this example we are only going to do 4 methods. Addition, subtraction, multiplication and division.

For all our other methods, we will do the same things but will change up the method name and the returned arithmetic. Arithmetic Operators in C#

public class Calculate
    {
        public int Add(int num1, int num2)
        {
            return (num1 + num2);
        }

        public int Sub(int num1, int num2)
        {
            return (num1 - num2);
        }

        public int Multiply(int num1, int num2)
        {
            return (num1 * num2);
        }

        public int Divide(int num1, int num2)
        {
            return (num1 / num2);
        }

    }

Building our API

Now that we are ready to use our API, firstly we need to build it! There are 2 ways we can build our project, we can either use the keyboard shortcut (Ctrl+B) or we can go to the Build tab and select Build Solution VS Build Project

To locate your new API, we will need to go through our project folder. Here is how to find it!

Step 1: Right-click your solution in Visual Studio and click on “Open Folder in File Explorer” VS Open Project Folder

Now that our file explorer is open, You’re going to want to navigate into the Debug folder as shown below

    • Once you have directed into the net5.0 folder, you will see your API file, it will look like

    • project.dll
    • File Displayed in File Explorer

      Using our API (Setup)

      Congratulations, you have now successfully made your first API! Now, its time to use it in another application!


      For this example, I will be making a C# Console Application Visual Studio new app

      Once our project that we want to use our API in is open, we’re going to want to right click on the Dependencies in the Solution Explorer and click on Add Project Reference Project Reference

      Once opened, you’re going to want to click the Browse button and navigate to your API’s folder. Project Reference

      Select our API file and add it to the project, then click OK.

      Interacting with our API

      Now that our API is a dependency in our new project, we’re going to want to start by “including” it in our class that we are using it in. Including API into the class

      Once that is done, we want to create our new “Calculate” class from our api with the following code.

      Calculate calc = new Calculate();

      Making the new class

      Now that we have “included” our API, we are ready to use it!

      Using our API (running methods)

      Inside of our program, we can use the field calc to refer to the API’s "Calculate" class.

      By using our calc variable, we can see our methods from our API inside of our project! Making the new class

      And so we know they work, here is an image of all the methods executing from our API inside of our project! Making the new class

      Calculate calc = new Calculate();
      
      Console.WriteLine("2 + 2 = " + calc.Add(2, 2));
      Console.WriteLine("5 - 16 = " + calc.Sub(5, 16));
      Console.WriteLine("8 x 5 = " + calc.Multiply(8, 5));
      Console.WriteLine("12 ÷ 2 = " + calc.Divide(12, 2));

      And there you have it!

      You have now made your very own API in C#, this was an overview of the basics of API development - although this was very simple, API’s can become very advanced and can be very useful in any circumstance!