Before posting my question, I would like to tell you that I have no prior experience in .Net technologies and have recently started to learn C# (and WPF). My company is looking to move onto .Net technologies and I am the only one in my team learning it, so have noone apart from you guys to discuss or ask something. So if my questions are too stupid or basic at best, please bear with me.
I was trying to create a generic linked list class to allow creation of linked lists of different types. I wrote the following code and would like to have your advise on whether I have written this code properly or not. Any suggestions to improve the code in any way is most welcome.
Main program
class Program
{
static void Main(string[] args)
{
GenLinkedList<string> list = new GenLinkedList<string>("abc");
list.AddtoList("def");
int i = 0;
string[] arr = new string[10];
list.LinkedList.CopyTo(arr,0);
for (i = 0; i < list.LinkedList.Count; i++)
{
Console.WriteLine(arr[i]);
}
GenLinkedList<int> listInt = new GenLinkedList<int>(1);
listInt.AddtoList(2);
i = 0;
int[] arrInt = new int[10];
listInt.LinkedList.CopyTo(arrInt, 0);
for (i = 0; i < listInt.LinkedList.Count; i++)
{
Console.WriteLine(arrInt[i]);
}
}
}
Class GenLinkedList
public class GenLinkedList<T>
{
private LinkedList<T> _linkedlist;
public GenLinkedList(T a)
{
_linkedlist = new LinkedList<T>();
_linkedlist.AddLast(a);
}
public LinkedList<T> LinkedList
{
get
{
return _linkedlist;
}
}
public void AddtoList(T a)
{
LinkedList.AddLast(a);
}
}
1
A generic linked list implementation already exists in the .NET framework:
LinkedList<T>. But you already know that; your code wraps it.2
OK, so you know that. Why would you wrap it, then? The only functionality you appear to have implemented is
AddtoList, which doesn’t do anything theLinkedList<T>doesn’t already do itself (after all, this is only a thin wrapper aroundLinkedList<T>.AddLast). What this means is that yourGenLinkedList<T>class really doesn’t offer the functionality of a linked list; it’s basically an add-only collection (which could just as easily have been implemented with aList<T>, or aStack<T>, or aQueue<T>— anything, really).3
Assuming you do have a good reason to wrap a
LinkedList<T>(e.g., you’re planning to add more functionality down the line that would actually leverage the behavior of aLinkedList<T>and/or — here’s a key ingredient — you want to restrict the way calling code is able to interact with the list (e.g., no removals)), you really shouldn’t expose yourLinkedList<T>member at all. The purpose of a wrapper is just that: to wrap. You take an existing class and basically give it a new kind of interface. By exposing the underlying object directly, you cripple your wrapper. Any additional restrictions/validation/logic you have in your wrapper can be bypassed.So, for example, if you want to be able to copy your list to an array, instead of doing this:
You would implement a
CopyTomethod within yourGenLinkedList<T>class (which could simply call_linkedlist.CopyTo) and use that.But I really think the first question you should be asking yourself is what you want to accomplish by wrapping
LinkedList<T>in the first place.