为什么C#不提供C++风格的'朋友'关键字?[英] Why does C# not provide the C++ style 'friend' keyword?



c ++ friend keyword 允许a class A class A a class B as它的朋友.这允许Class B访问private/protected class A的成员.

我从来没有读过任何关于为什么c#(和vb.net)排除的内容.此早期的stackoverflow问题似乎是在说这是一个,这似乎是说这是一个C ++的有用部分,有充分的理由使用它.根据我的经验,我必须同意.



因此,总而言之,为什么friend c#中缺少friend在c#?


(顺便说一句,internal关键字不是不是,它允许整个汇编内的 all> All 类访问internal成员,而friend允许您给出某个类别完全访问 恰恰是一个其他类)



话虽如此,这是一个不错的功能,我在C ++中自己使用了很多次.并且也想在C#中使用它.但是我敢打赌,因为c#的" pure" ooness(与C ++的pseudo ooness相比)MS决定,因为Java没有朋友关键字C#不应该(只是开玩笑;))




私人和受保护的变量和方法可能是OOP最重要的部分之一.对象可以持有只有它们可以使用的数据或逻辑的想法使您可以独立于环境编写功能的实现 - 并且您的环境不能更改不适合处理的状态信息.通过使用朋友,您将两个类的实现耦合在一起 - 这要糟糕得多.


在侧面注释上. 使用朋友并不是要违反封装,而是相反,它是关于强制执行的.像访问者+突变器一样,操作员过载,公共继承,降低,等.

参见 konrad rudolph '/should-you-use-friend-in-c#17505">消息在另一个线程中,或者如果您喜欢请参见相关条目在C ++常见问题中.


有关信息,.NET中的另一个相关但不合时宜的东西是[InternalsVisibleTo],它可以让一个组件指定另一个组件(例如一个单位测试组件)(有效地) "内部"对原始组件中的类型/成员的访问.



The C++ friend keyword allows a class A to designate class B as its friend. This allows Class B to access the private/protected members of class A.

I've never read anything as to why this was left out of C# (and VB.NET). Most answers to this earlier StackOverflow question 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.

Another question seems to me to be really asking how to do something similar to friend in a C# application. While the answers generally revolve around nested classes, it doesn't seem quite as elegant as using the friend keyword.

The original Design Patterns book uses it regularly throughout its examples.

So in summary, why is friend missing from C#, and what is the "best practice" way (or ways) of simulating it in C#?

(By the way, the internal keyword is not the same thing, it allows all classes within the entire assembly to access internal members, while friend allows you to give a certain class complete access to exactly one other class)


Having friends in programming is more-or-less considered "dirty" and easy to abuse. It breaks the relationships between classes and undermines some fundamental attributes of an OO language.

That being said, it is a nice feature and I've used it plenty of times myself in C++; and would like to use it in C# too. But I bet because of C#'s "pure" OOness (compared to C++'s pseudo OOness) MS decided that because Java has no friend keyword C# shouldn't either (just kidding ;))

On a serious note: internal is not as good as friend but it does get the job done. Remember that it is rare that you will be distributing your code to 3rd party developers not through a DLL; so as long as you and your team know about the internal classes and their use you should be fine.

EDIT Let me clarify how the friend keyword undermines OOP.

Private and protected variables and methods are perhaps one of the most important part of OOP. The idea that objects can hold data or logic that only they can use allows you to write your implementation of functionality independent of your environment - and that your environment cannot alter state information that it is not suited to handle. By using friend you are coupling two classes' implementations together - which is much worse then if you just coupled their interface.


On a side note. Using friend is not about violating the encapsulation, but on the contrary it's about enforcing it. Like accessors+mutators, operators overloading, public inheritance, downcasting, etc., it's often misused, but it does not mean the keyword has no, or worse, a bad purpose.

See Konrad Rudolph's message in the other thread, or if you prefer see the relevant entry in the C++ FAQ.


For info, another related-but-not-quite-the-same thing in .NET is [InternalsVisibleTo], which lets an assembly designate another assembly (such as a unit test assembly) that (effectively) has "internal" access to types/members in the original assembly.