# Libraries - Introduction to C#

## Libraries

C# includes many predefined functions and libraries that developers can use to accomplish various tasks more easily and efficiently. The .NET Framework provides these libraries and includes functionalities for things like file I/O, database access, networking, and much more.

A library in C# is typically provided as a `.dll` (Dynamic Link Library) file. To use the library's functions and classes, you must first reference it in your project. This will be done automatically if the library is installed via a package manager like nuget, or if you use a library from within the .NET ecosystem.

The `using` directive then tells the compiler to use a specific namespace in the library. A `namespace` groups related class, structures, and other types under a single name. For instance, the `System` namespace includes fundamental classes and base types that are used in C# programming.

For example, to use the `File` class from the [`System.IO`](http://System.IO) namespace for handling files, you would first need to add `using` [`System.IO`](http://System.IO)`;` at the top of your code.

```csharp
using System.IO;

class Program
{
    static void Main(string[] args)
    {
        // Check if a file exists
        if (File.Exists("test.txt"))
        {
            // Read the content of the file
            string content = File.ReadAllText("test.txt");
            Console.WriteLine(content);
        }
        else
        {
            Console.WriteLine("The file does not exist.");
        }
    }
}
```

In this example, `File.Exists` is a predefined function from the [`System.IO`](http://System.IO) namespace that checks if a file exists at the provided path and `File.ReadAllText` is another predefined function that reads the entire content of the file as a string. Because `System` is a core library, the compiler will automatically include it.

Similarly, you can use predefined functions from other namespaces and libraries—for instance, the `System.Math` namespace contains mathematical functions such as `Math.Sqrt` for computing the square root of a number, `Math.Pow` for raising a number to a specified power, and `Math.Round` for rounding a number to the nearest integer.

```csharp
using System;

class Program
{
    static void Main(string[] args)
    {
        double num = 9.0;
        double squareRoot = Math.Sqrt(num);
        Console.WriteLine($"The square root of {num} is {squareRoot}");

        double baseNum = 2.0;
        double exponent = 3.0;
        double power = Math.Pow(baseNum, exponent);
        Console.WriteLine($"{baseNum} raised to the power of {exponent} is {power}");

        double toBeRounded = 9.5;
        double rounded = Math.Round(toBeRounded);
        Console.WriteLine($"{toBeRounded} rounded to the nearest integer is {rounded}");
    }
}
```

As you can see, leveraging the predefined functions and libraries provided by the .NET Framework can achieve complex functionality with less code.

## **NuGet**

In addition to the standard libraries, C# offers extensive support for using third-party libraries and packages. These can be added to your project through various means, including the [NuGet package manager](https://www.nuget.org/). `NuGet` is a free and open-source package manager designed for the Microsoft development platform, and it hosts thousands of libraries.

Adding a `NuGet` package to your project can be as easy as right-clicking on your project in the Solution Explorer in Visual Studio, selecting "`Manage NuGet Packages...`" and then searching for and installing the required package. If using a code editor, we use the `dotnet package add` command, but [Microsoft provides great documentation for using nuget from the CLI](https://learn.microsoft.com/en-za/nuget/consume-packages/install-use-packages-dotnet-cli).

Once a package is installed, you can utilise its functionality in your code by adding the appropriate `using` directive at the top of your file. The `Newtonsoft.Json` package, for instance, provides powerful tools for working with JSON data.

```csharp
using Newtonsoft.Json;
using System;
using System.Collections.Generic;

class Program
{
    static void Main(string[] args)
    {
        string json = "[{'Name':'John', 'Age':30}, {'Name':'Jane', 'Age':28}]";

        List<Person> people = JsonConvert.DeserializeObject<List<Person>>(json);

        foreach (var person in people)
        {
            Console.WriteLine($"Name: {person.Name}, Age: {person.Age}");
        }
    }
}

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
}
```

In this example, the `JsonConvert.DeserializeObject<T>` method is used to parse the JSON string into a list of `Person` objects. This predefined function, part of the `Newtonsoft.Json` library, dramatically simplifies the task of JSON parsing.

## **Manual Referencing**

It is also possible to manually link a library to the project. If you use an IDE such as Visual Studio, or Jetbrains Rider, it's as simple as right-clicking on the `Project Dependencies` section under the solution explorer and selecting the `Add Project Reference...` option, and then finding the library you want to link.

Alternatively, if you are using a Code editor, such as VSCode, you will need to manually edit the project file to include the references, such as the example below, which is going to reference every `.dll` file in the libs subfolder:

```xml
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net7.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
  </PropertyGroup>
  <ItemGroup>
    <!-- references look like this -->
    <Reference Include="libs\*.dll" /> 
  </ItemGroup>

</Project>
```

It's also possible to hardcode paths and establish multiple `Reference` definitions for each individualreference specifically.

To identify the `namespaces`, `types`, `classes`, and `methods` provided by the library, it is generally considered best practice to consult the provided documentation. Both Visual Studio and Visual Studio Code will provide code auto-complete functionality for the functionality from imported libraries through their IntelliSense auto-complete tool.

While the .NET Framework and third-party libraries offer a wide array of predefined functions, it's essential to understand their usage and potential impact on your application. Some libraries may have licensing restrictions, or they may not be maintained actively. Always research before including a third-party library in your project.

<details data-node-type="hn-details-summary"><summary>Questions</summary><div data-type="detailsContent">Import the Library-Question library appropriate for your OS and dotNet version, using the HTBLibrary namespace. What is the output of the Flag.GetFlag() method from the library?</div></details>![](https://cdn.hashnode.com/res/hashnode/image/upload/v1764178807726/999b25ea-9569-4aa8-8005-69a976decdba.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1764178832524/519c0427-c164-422c-9eff-9131798122ae.png align="center")

### \==&gt; Key Lessons Learned from the Exercise

* Distinguish between applications and libraries
    
    * A `.dll` file is usually a library, not a standalone application.
        
    * You cannot run it directly with `dotnet run`; instead, you must create a separate project and reference the library.
        
* Correct `.csproj` structure
    
    * A project file must have a single root `<Project>` element.
        
    * All `<ItemGroup>` and `<Reference>` entries must be placed inside this root.
        
    * Misplaced XML tags cause build errors like *“multiple root elements.”*
        
* Namespaces and `using` directives
    
    * Adding a DLL is not enough; you must know the correct namespace it exposes.
        
    * If you use the wrong namespace, the complier will throw *“type or namespace not found”.*
        
* Managing .NET versions
    
    * You saw warnings about `.NET 7.0` being out of support.
        
    * This teaches you to check the framework lifecycle and ensure your project targets a supported version.
        
    * Use `.csproj` or `global.json` to lock your project to a specific SDK/runtime.
        
* Debugging mindset
    
    * Error messages are your guide:
        
        * *hostpolicy.dll not found → you tried to run a library as an app.*
            
        * *multiple root elements → XML structure issue.*
            
        * *namespace not found → missing or incorrect reference.*
            
    * Reading and interpreting these messages is a critical skill for solving problems quickly.
        
* Practical integration skills
    
    * In real-world projects, you often need to integrate external libraries (via NuGet or DLLs).
        
    * Knowing how to reference them, configure `.csproj`, and resolve build errors is essential for professional development..
