Day 29 of 30-Day .NET Challenge: Generics & Custom Interfaces
Tags: dotnet, csharp, programming, beginners
Published: 2024-04-19 05:50:00

Learn to enhance your maintainability with generics and custom interfaces in C#. Discover a better approach on Day 29 of our 30-Day .NET Challenge.

## Introduction

Developers often tend to add unnecessary boxing in the code which can hamper application performance. The article demonstrates both inefficient and efficient approaches to avoid the issue.

### Learning Objectives

* What is Boxing

* Why non-generic interface approach is inefficient

* A recommended approach using generics

### Prerequisites for Developers

* Basic understanding of C# programming language.

[30 Day .Net Challenge](https://singhsukhpinder.medium.com/list/52a751260fe1)

## Getting Started

### What is Boxing

Boxing is the process of converting a value type into an object type. In other words, it means the allocation of objects on a heap rather than a stack. Therefore, a performance overhead because of increased memory usage and the need for garbage collection.

### Why non-generic interface approach is inefficient

In the following scenario, each time a value is assigned to values, it undergoes boxing which can make your application suffer from performance pressure.
```csharp
    public interface INumber
    {
        object Value { get; set; }
    }
    
    public class Number : INumber
    {
        public object Value { get; set; }
    }
```
### A recommended approach using generics

Please find below the refactored version of the previous code snippet using generics which allows type safety without the need for boxing.
```csharp
    public interface INumber<T>
    {
        T Value { get; set; }
    }
    
    public class Number<T> : INumber<T> // Utilize generics to avoid boxing
    {
        public T Value { get; set; }
    }
```
### How to use Generics in practice
```csharp
    var intNumber = new Number<int> { Value = 123 };
    var floatNumber = new Number<float> { Value = 123.45f };
```
## Complete Code

Create another class named GenericCustomInterfaces and add the following code snippet
```csharp
    public static class GenericCustomInterfaces
    {
      static List<INumber<int>> intNumbers = new List<INumber<int>>();
      static List<INumber<double>> doubleNumbers = new List<INumber<double>>();
    
      public static void Example()
      {
          // Populate the list with integers
          for (int i = 0; i < 10; i++)
          {
              intNumbers.Add(new Number<int>(i));
          }
    
          // Populate the list with doubles
          for (double d = 0.5; d < 10.0; d += 1.0)
          {
              doubleNumbers.Add(new Number<double>(d));
          }
    
          // Process and display integer numbers
          Console.WriteLine("Integer Numbers:");
          foreach (var num in intNumbers)
          {
              Console.WriteLine(num.Value);
          }
    
          // Process and display double numbers
          Console.WriteLine("\nDouble Numbers:");
          foreach (var num in doubleNumbers)
          {
              Console.WriteLine(num.Value);
          }
      }
    }
```
Add the interface and its class implementation as follows
```csharp
    public interface INumber<T>
    {
        T Value { get; set; }
    }
    
    public class Number<T> : INumber<T>
    {
        public T Value { get; set; }
        public Number(T value)
        {
            Value = value;
        }
    }
```
## Execute from the main method as follows
```csharp
    #region Day 29: Generics & Custom Interfaces
    static string ExecuteDay29()
    {
        GenericCustomInterfaces.Example();
    
        return "Executed Day 29 successfully..!!";
    }
    
    #endregion
```
## Console Output
```
    Integer Numbers:
    0
    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    Double Numbers:
    0.5
    1.5
    2.5
    3.5
    4.5
    5.5
    6.5
    7.5
    8.5
    9.5
```
## Complete Code on GitHub
[GitHub — ssukhpinder/30DayChallenge.Net](https://github.com/ssukhpinder/30DayChallenge.Net/tree/main)

### C# Programming🚀

Thank you for being a part of the C# community! Before you leave:

Follow us: [Youtube](https://www.youtube.com/channel/UCejYGg4gAOa1uPAObFuk1HQ) | [X](https://twitter.com/sukhsukhpinder) | [LinkedIn](https://www.linkedin.com/in/sukhpinder-singh/) | [Dev.to](https://dev.to/ssukhpinder) 
Visit our other platforms: [GitHub](https://github.com/ssukhpinder) 
More content at [C# Programming](https://medium.com/c-sharp-progarmming)