I'm implementing a union-find data structure in C#. The elements must extend the Element inner-class, but I'd like to to keep the fields in that class private to the outside world. They need to be public to the direct outer-class, however. The folloowing code does not compile due to "inconsistent accessibility":
class DisjointSetForrests<T> where T : DisjointSetForrests<T>.Element {
private class PrivateElement {
public Element p;
public int rank;
}
public class Element : PrivateElement {
}
public void MakeSet(T x) {
x.p = x;
x.rank = 0;
}
public T FindSet(T x) {
if (x != x.p) x.p = FindSet(x);
return (T)x.p;
}
public void Union(T x, T y) {
Link(FindSet(x), FindSet(y));
}
public void Link(T x, T y) {
if (x.rank > y.rank) {
y.p = x;
} else {
x.p = y;
if (x.rank == y.rank) y.rank++;
}
}
}
Is there a way to achieve what I want, or should I accept the fields in Element being public?
It's not possible to keep them only public to the outer class.
The question is why do you need to keep them public? If you creating a library you could use internal
.