Generic Types
Explore how to implement generic types in C# to design versatile classes that handle multiple data types. Understand type parameters, default values, and how generics help avoid code duplication for better object-oriented programming.
We'll cover the following...
We'll cover the following...
Introduction
Consider the following Holder class:
public class Holder
{
public string[] Items { get; private set; }
public Holder(int holderSize)
{
Items = new string[holderSize];
}
public override string ToString()
{
string result = "Items inside: ";
foreach (var item in Items)
{
result = result + item + " ";
}
return result;
}
}
It has the Items property, which is string type. The Holder class holds string items. What if we need a similar class that holds integers? The functionality is the same, but the type is different. ...