I have two repository classes (RepositoryFactory and BaseRepository) implementing different interfaces within the same project. The BaseRepository class has a private method that would be now needed also in the other class, with the same implementation.
Instead of duplicate the method in order to keep it private, I was thinking to a possible alternative, although so far I could not find a good solution since by definition a private method has scope only in its own class.
Using inheritance and change the method to “protected” would also not be an option, since the two classes are not linked semantically. I cannot use a public property giving back the result of the method since the return type is void.
It’s not possible to do what you want in C#. The closest you can have is
internal, which makes the member visible to an entire assembly. It might also be possible to make the two classes private and nested inside another class, but this isn’t always appropriate.Mads Torgersen, who works on C#, has this to say about it:
(source)