Ok, let’s leave the debate of whether friendship breaks encapsulation, and actually try elegantly come up with a coherent design. It is a two fold function:
1) General question on how to implement:
public class A { friend class B; }
2) Why do I need this functionality? Some of my classes implement ISerializable interface. However, I want to make ISerializable methods protected in the Derived class so that I don’t expose them to a client (as well as in the documentation). However, internal classes should be able to access them. What is the General way to solve this problem in C#?
Note: I am using friendship as defined in the current C++ standard.
Thanks
Leaving the
InternalsVisibleTostuff to one side, you only have two choices when it comes to implementing interfaces:In both cases anyone can call the methods, but using explicit interface implementation you can only call the methods ‘via’ an interface expression (e.g. you could cast a variable of the actual type to the
ISerializable).There’s no such concept as ‘internally’ implementing an interface.