Would there be any problems if I did the following to imitate a liked list using a list.
Create a class like this
public class MyClass
{
//some properties
public MyClass Previous {get; set;}
public MyClass Next {get; set;}
}
and add this to a generic List.
I only add elements to this list and never remove them.
I am not using the .Net LinkedList class as I had overlooked the Previous property of the LinkedListNode class. I was in a hurry to get things working.
The way I am using this class is mostly by using the ForEach extension of the List.
List<MyClass> MyList = new List<MyClass>();
//add some elements with Previous,Next set
MyList.Foreach(CalculateValues);
CalculateValues(MyClass current)
{
MyClass prev = current.Previous;
//check for null and return etc
//In some moethods I use Next
current.SomeProperty += prev.SomeProperty;
}
I see that the LinkedList has no ForEach method though. I could still iterate using ForEach loop.
Thanks for all the great answers.
Yes, you would be violating the Single Responsibility Principle by using the same class to represent both a Collection of an item and the item itself. And you wouldn’t be making use of the perfectly-good classes that come standard with the .NET library, like
LinkedList<T>.