C#C
C#11mo ago
Faker

✅ How can I check if an array need to be resize when adding an element

C#
class CustomList<T>
{
    private T[] items;
    private int size;

    public CustomList()
    {
        items = new T[10]; // initial capacity
        size = 0;
    }

    // Add method
    public void Add(T item)
    {
        // Check if resizing is needed
        
        // Add item to the list
    }

Hello guys, can someone suggest an idea of how to check whether the array is full and need to be resize when adding an element please. Since the array is a generic type and can have any data type, I can't check if the last index of the array contains a specific default value of a data type, like I can't assume it's an int and so have a default value of 0. Moreover, I don't think it would be right to assume 0 is a default value since our array can have a value of 0 as its element.
Was this page helpful?