Because sometimes, I really need a friend.
I can think of the following tricks:
internal
.System.ComponentModel.EditorBrowsable(System.ComponentModel.
EditorBrowsableState.Never)
attribute to the member you want only the friend to access to hide it from IntelliSense.Is this list exhaustive? Can anyone sort these in order of decreasing performance? Order of decreasing neatness? Any suggestions when to use which?
You can also use the InternalsVisibleTo attribute.
For a given assembly, A, you can specify which other assemblies can have access to A's internal types.
In c# Nested classes (like private classes) are similar to friend in c++:
public class Root
{
private int a; // accessible for friendroot
public int b;
public class FriendOfRoot
{
public int d;
}
}
Edit: If the simulation of friend with nested classes provided here is useful for you, in performance it's fast enough like regular classes (In compile all things will be determined and there is no casting issues and no overhead).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With