Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy does C# not provide the C++ style 'friend' keyword?
    primarykey
    data
    text
    <p>The <a href="http://www.cplusplus.com/doc/tutorial/inheritance/" rel="noreferrer">C++ friend keyword</a> allows a <code>class A</code> to designate <code>class B</code> as its friend. This allows <code>Class B</code> to access the <code>private</code>/<code>protected</code> members of <code>class A</code>.</p> <p>I've never read anything as to why this was left out of C# (and VB.NET). Most answers to this <a href="https://stackoverflow.com/questions/17434/when-should-you-use-friend-in-c">earlier StackOverflow question</a> seem to be saying it is a useful part of C++ and there are good reasons to use it. In my experience I'd have to agree.</p> <p>Another <a href="https://stackoverflow.com/questions/200079/inheritance-trees-and-protected-constructors-in-c#200117">question</a> seems to me to be really asking how to do something similar to <code>friend</code> in a C# application. While the answers generally revolve around nested classes, it doesn't seem quite as elegant as using the <code>friend</code> keyword.</p> <p>The original <a href="http://rads.stackoverflow.com/amzn/click/0201633612" rel="noreferrer">Design Patterns book</a> uses it regularly throughout its examples.</p> <p>So in summary, why is <code>friend</code> missing from C#, and what is the "best practice" way (or ways) of simulating it in C#?</p> <p>(By the way, the <code>internal</code> keyword is <em>not</em> the same thing, it allows <em>all</em> classes within the entire assembly to access <code>internal</code> members, while <code>friend</code> allows you to give a certain class <em>complete access</em> to <em>exactly one</em> other class)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload