I am implementing a Linked list in C#. I created a Node class and a MyLinkedList class. Node class implements the node having properties Key and Next. MyLinkedList class implements all the operations such as Insert, Find, Update, Remove etc and two properties Head and Count.
I don't want the user to create Node instances from outside of the MyLinkedList class so I nested the Node class inside MyLinkedList class making it private. Now, in the implemetation of Find method I am unable to return the Node type. I am also not able to give public access to Head property of outer class which is also a Node instance.
Summary: Basically, the problem is I want to prevent user from making an object of nested private class anywhere outside of it's enclosing class but it also limits the enclosing class ability to return it's nested private class' instance in the process.
public class MyLinkedList<T>
{
public Node<T> Head { get; set; } //Error: Inconsistent accessibility: property type 'MyLinkedList<T>.Node<T>' is less accessible than property 'MyLinkedList<T>.Head'
public int Count { get; set; }
public MyLinkedList()
{
this.Head = null;
this.Count = 0;
}
public Node<T> Find(T value)//Error: Inconsistent accessibility: property type 'MyLinkedList<T>.Node<T>' is less accessible than method 'MyLinkedList<T>.Find(T)'
{
Node<T> curr = this.Head;
while (!(curr is null) && !curr.Data.Equals(value))
{
curr = curr.Next;
}
if (curr == null) return null;
return curr;
}
private class Node<K>
{
public K Data { get; set; }
public Node<K> Next { get; internal set; }
public Node(K data)
{
this.Data = data;
this.Next = null;
}
}
}