Share via

Generic List

Ronald Rex 1,671 Reputation points
2022-07-21T18:43:21.647+00:00

Hi friends. I am trying to debug this code so that I can see the value that I added to the variable list1, but when I hover over the variable after adding 1 to the list it doesnt show me anything. Am I debugging this Generic wrong?

 public class GenericList<T>  
        {  
            public void Add(T input) { }  
        }  
        
    class TestGenericList  
    {  
         static void Main()  
        {  
            // Declare a list of type int.  
            GenericList<int> list1 = new GenericList<int>();  
            list1.Add(1);  
        }  
    }  
  
  
  
  
Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.

0 comments No comments

Answer accepted by question author

Karen Payne MVP 35,606 Reputation points Volunteer Moderator
2022-07-21T20:27:30.333+00:00

This will work, no need to create an Add method and if so you are hiding the base Add method.

using System;  
using System.Collections.Generic;  
  
namespace ForumQuestion  
{  
    partial class Program  
    {  
        private static readonly GenericList<int> _list = new();  
        static void Main(string[] args)  
        {  
  
            for (int index = 0; index < 5; index++)  
            {  
                _list.Add(index);  
            }  
  
            Console.WriteLine(string.Join(",", _list));  
            Console.ReadLine();  
        }  
    }  
  
    public class GenericList<T> : List<T> { }  
}  
  

Was this answer helpful?

0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Reza Aghaei 4,996 Reputation points Volunteer Moderator
    2022-07-21T21:36:11.183+00:00

    There is a generic list implemented in .NET, List<T>, you even do not need to derive from it. Just use it like this:

    var list = new List<int>();  
    list.Add(1);  
    

    Then the list will contain an item.

    If this is an assignment or for learning purpose you want to implement a generic list class from scratch then you can take a look at source code of List<T> or you can extend the following code:

    using System;  
    public class MyList<T>  
    {  
        private T[] items = new T[] { };  
        public void Add(T item)  
        {  
            T[] newItems = new T[items.Length + 1];  
            if (items.Length > 0)  
                Array.Copy(items, 0, newItems, 0, items.Length);  
            items = newItems;  
            items[items.Length - 1] = item;  
        }  
        public void RemoveAt(int index)  
        {  
            if (index > items.Length - 1)  
                throw new IndexOutOfRangeException();  
            if (index < items.Length - 1)  
            {  
                Array.Copy(items, index + 1, items, index, items.Length - index - 1);  
    
            }  
            T[] newItems = new T[items.Length - 1];  
            if (items.Length - 1 > 0)  
                Array.Copy(items, 0, newItems, 0, items.Length - 1);  
            items = newItems;  
        }  
        public T this[int index]  
        {  
            get  
            {  
                return items[index];  
            }  
            set  
            {  
                items[index] = value;  
            }  
        }  
    }   
    

    Was this answer helpful?

    0 comments No comments

  2. Bruce (SqlWork.com) 84,086 Reputation points
    2022-07-21T21:28:05.757+00:00

    your code:

             public class GenericList<T>  
             {  
                 public void Add(T input) { }  
             }  
    

    the Add method doesn't do anything passed input. as the class has no public properties, there is nothing for the debugger to display; typically a list class would implement IEnumerable<T> interface.

        public class GenericList<T> : IEnumerable<T>  
        {  
            private T[] items = new T[] { };  
      
            public GenericList() { }  
      
            public void Add(T input)  
            {  
                items = items.Append(input).ToArray();  
            }  
      
            public T Get(int i) => items[i];  
      
            public int Count => items.Length;  
      
            public IEnumerator<T> GetEnumerator() => new Enumerator(this);  
              
            IEnumerator IEnumerable.GetEnumerator() => new Enumerator(this);  
      
            public class Enumerator : IEnumerator<T>  
            {  
                private GenericList<T> list;  
                private int index = -1;  
      
                internal Enumerator(GenericList<T> items)  
                {  
                    list = items;  
                }  
      
                public T Current => list.Get(index);  
      
                object? IEnumerator.Current => Current;  
      
                public void Dispose() { }  
      
                public bool MoveNext() => ++index < list.Count;  
      
                public void Reset()  
                {  
                    index = -1;  
                }  
            }  
        }  
    

    Was this answer helpful?

    0 comments No comments

  3. Viorel 127K Reputation points
    2022-07-21T18:54:47.39+00:00

    For example, try this:

    public class GenericList<T> : List<T>  
    {  
    }  
    

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.