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

查看:125
本文介绍了为什么C#不提供C ++风格的“朋友”关键字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C ++ friend关键字允许类A 指定 class B 作为其朋友。这允许 B类访问 private / protected members的 class A



我从来没有读过任何关于为什么这被省略了C#(和VB 。净)。这个的大部分答案似乎都在说C ++的有用部分,有很好的理由使用它。根据我的经验,我必须同意。



另一个问题似乎在C#应用程序中真正要问如何做类似于 friend 的某些事情。虽然答案通常围绕嵌套类进行,但它似乎并不像使用朋友关键字那样优雅。



原始设计模式书籍在其示例中定期使用。



总而言之,为什么C#中缺少朋友,而且,最好的做法(或方式)是将其模拟C#?



(顺便说一下,内部关键字是不是 ,它允许整个程序集中的所有类访问内部成员,而朋友允许您将某一类完整访问 完全一个其他类)

解决方案

编程中的朋友被认为是肮脏的,容易被滥用。它打破了类之间的关系,并破坏了OO语言的一些基本属性。



说到这是一个很好的功能,我已经使用了很多次自己在C ++中并且也想在C#中使用它。但是我打赌是因为C#的纯粹OOness(与C ++的伪OOness相比)MS决定因为Java没有朋友关键字C#不应该(只是开玩笑))



<在一个严肃的说明:内部不如朋友那么好,但它确实完成了工作。记住,很少有人会将代码分发给第三方开发人员,而不是DLL;只要你和你的团队知道内部课程和他们的使用,你应该是好的。



编辑让我澄清一下朋友关键字破坏了OOP。



私有和受保护的变量和方法可能是OOP最重要的部分之一。对象可以容纳只能使用数据或逻辑的想法允许您独立于环境编写功能实现,而且您的环境无法更改不适合处理的状态信息。通过使用朋友,您将两个类的实现结合在一起 - 这是更糟的是,如果你刚刚耦合他们的界面。


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 though 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.

这篇关于为什么C#不提供C ++风格的“朋友”关键字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆