I’m trying to create a simple way to have my linkedlist act like a circular linked list in C#.
I have this code, and it does not want to show up as an extension method. Any idea why?
static class CircularLinkedList
{
public static LinkedListNode<object> NextOrFirst(this LinkedListNode<object> current)
{
if (current.Next == null)
return current.List.First;
return current.Next;
}
public static LinkedListNode<object> PreviousOrLast(this LinkedListNode<object> current)
{
if (current.Previous == null)
return current.List.Last;
return current.Previous;
}
}
This works fine for me in the following application:
Make sure that you are instantiating your LinkedListNode with the proper generic type , as using any other type (i.e. string) will not work. However, you can extend this to support any type of LinkedListNode by changing your code to the following: