Day 29 of 30-Day .NET Challenge: Generics & Custom Interfaces

Day 29 of 30-Day .NET Challenge: Generics & Custom Interfaces

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

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.

public interface INumber
{
    object Value { get; set; }
}

public class Number : INumber
{
    public object Value { get; set; }
}

Please find below the refactored version of the previous code snippet using generics which allows type safety without the need for boxing.

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

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

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

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

#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


C# Programming🚀

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

Follow us: Youtube | X | LinkedIn | Dev.to
Visit our other platforms: GitHub
More content at C# Programming

Did you find this article valuable?

Support C# Programming by becoming a sponsor. Any amount is appreciated!