# simple implementation of List in c#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class List<T>
{
private T[] arrayList;
private int arrayLenght = 5;
private int arrayIndex = 0;

public List()
{
arrayList = new T[arrayLenght];
}

internal string[] toArray()
{
string[] arrayListArray = new string[arrayLenght];
try
{

for (int j = 0; j < arrayList.Length; j++)
{
if (arrayList[j] == null)
{
arrayListArray[j] = "";
}
else
{
arrayListArray[j] = arrayList[j].ToString();
}

}
}
catch(Exception Ex)
{
Console.WriteLine(Ex);
}
return arrayListArray;
}

private void ArrayOverFlow(T genericParameter)
{
try
{
arrayLenght = 2 * arrayLenght;
T[] arrayListNewSize = new T[arrayLenght];
int arrayListNewSizeIndex = 0;
Array.Copy(arrayList, arrayListNewSize, arrayList.Length);
arrayList = new T[arrayLenght];
Array.Copy(arrayListNewSize, arrayList, arrayListNewSize.Length);
arrayListNewSize = null;
arrayList[arrayIndex] = genericParameter;
arrayIndex++;
}
catch (Exception Ex)
{
Console.WriteLine(Ex);
}
}

{
// Console.WriteLine("Parameter type: {0}, value: {1}", typeof(T).ToString(), genericParameter);
try
{
if (arrayIndex + 1 <= arrayLenght)
{
arrayList[arrayIndex] = genericParameter;
arrayIndex++;
}
else
{
ArrayOverFlow(genericParameter);
}
}
catch (Exception Ex)
{
Console.WriteLine(Ex);
}
}

internal void Insert(int index, T genericParameter)
{
try
{
arrayList[index] = genericParameter;
}
catch (Exception Ex)
{
Console.WriteLine(Ex);
}
}

private void RemoveArray(int index)
{
try
{
for (int i = index; i < arrayList.Length; i++)
{
if (i < arrayList.Length - 1)
{
arrayList[i] = arrayList[i + 1];
}
}
}
catch (Exception Ex) {
Console.WriteLine(Ex);
}
}

internal void RemoveAt(int index)
{
RemoveArray(index);
}

internal void Remove(T genericParameter)
{
try
{
for (int i = 0; i < arrayList.Length; i++)
{
if (arrayList[i] != null)
{
if (arrayList[i].Equals(genericParameter))
{
RemoveArray(i);
}
}
}
}
catch (Exception Ex)
{
Console.WriteLine(Ex);
}
}
}
}


I have also written test cases for the same

using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
[TestFixture]
class Test1
{
[TestCase]
{
List<int> st = new List<int>();

Assert.AreEqual("10,20,30,40,50,60,0,0,0,0", string.Join(",", st.toArray()));
}
[TestCase]
{
List<string> st = new List<string>();
Assert.AreEqual("10,20,30,40,50,60,,,,", string.Join(",", st.toArray()));
}

[TestCase]
{
List<int> st = new List<int>();
st.Insert(2, 70);
Assert.AreEqual("10,20,70,40,50,60,0,0,0,0", string.Join(",", st.toArray()));
}

[TestCase]
{
List<string> st = new List<string>();
st.Insert(2, "70");
Assert.AreEqual("10,20,70,40,50,60,,,,", string.Join(",", st.toArray()));
}

[TestCase]
{
List<int> st = new List<int>();
st.RemoveAt(2);
Assert.AreEqual("10,20,40,50,60,0,0,0,0,0", string.Join(",", st.toArray()));
}

[TestCase]
{
List<string> st = new List<string>();
st.RemoveAt(2);
Assert.AreEqual("10,20,40,50,60,,,,,", string.Join(",", st.toArray()));
}
[TestCase]
{
List<int> st = new List<int>();
st.Remove(30);
Assert.AreEqual("10,20,40,50,60,0,0,0,0,0", string.Join(",", st.toArray()));
}

[TestCase]
{
List<string> st = new List<string>();
st.Remove("30");
Assert.AreEqual("10,20,40,50,60,,,,,", string.Join(",", st.toArray()));

}
}
}


I am learning data structure so I tried to implement a list for better understanding so any feedback and code review would be helpful.

• Does the current code work as intended?
– Mast
Mar 23, 2020 at 18:28
• At least the sequence of letters in arrayLenght looks consistent. Who is to judge works as intended? No intention is documented. Insert(i, T) looks weird; RemoveArray(i) a reference hog. Mar 23, 2020 at 21:29
• the above code works as expected so can you tell me what did look weird in insert in Insert Mar 30, 2020 at 5:35
• Please do not update the code in your question to incorporate feedback from answers, doing so goes against the Question + Answer style of Code Review. This is not a forum where you should keep the most updated version in your question. Please see what you may and may not do after receiving answers.
– Mast
Mar 30, 2020 at 6:16

Your Insert is wrong, you have to do it the same way as the Remove with shifting Elements. ArrayIndex is not increased to decreased on Remove/Insert.

Better don't call any parameter a parameter or add the DataType to it. Name as you speak. You insert an 'item' or an 'element' to your list. Not a parameter.

Always Doubling the array on overflow can lead to very huge arrays. Imagine you have a 1 Gigabyte Array and you want to add 1 element,you allocate 1 new gigabyte for that. It's better to increase by a fixed amount of items, like 16 or may 1024.

Why do you have to have two Array.Copy on reallocation ? Usual approach is to create a new array and copy existing elements.

var tmp = new T[newarraylength];
Array.Copy(arrayList, tmp, oldarraylength);
arrayList = tmp;


I don't get why you copy something twice.

• I have edited my code as you said for array copy but as you said the insert code is wrong but .i have see code codereview.stackexchange.com/questions/144360/… which see the same as mine. Mar 30, 2020 at 5:43
• No, the other code is fine. It does not have any Insert at all, and the place with two Array.Copy handles two sections of the same array. Sure I'm just assuming what insert means, for me it's adding an element in the middle. What you do is replacing an element. Mar 31, 2020 at 7:53