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.
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.
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.
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; }
}
var intNumber = new Number<int> { Value = 123 };
var floatNumber = new Number<float> { Value = 123.45f };
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;
}
}
#region Day 29: Generics & Custom Interfaces
static string ExecuteDay29()
{
GenericCustomInterfaces.Example();
return "Executed Day 29 successfully..!!";
}
#endregion
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 here: GitHub — ssukhpinder/30DayChallenge.Net
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