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

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

问题描述

C++朋友关键字允许A类指定 class B 作为它的朋友.这允许 Class B 访问 class Aprivate/protected 成员.

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.

我从来没有读过任何关于为什么它被排除在 C#(和 VB.NET)之外的信息.这个早期的StackOverflow问题的大多数答案似乎都在说这是一个C++ 的有用部分,并且有充分的理由使用它.根据我的经验,我必须同意.

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.

另一个问题在我看来真的是在问如何在 C# 应用程序中执行类似于 friend 的操作.虽然答案通常围绕嵌套类展开,但它似乎没有使用 friend 关键字那么优雅.

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.

总而言之,为什么 C# 中缺少 friend,以及在 C# 中模拟它的最佳实践"方式是什么?

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

(顺便说一下,internal 关键字不是一样的东西,它允许整个程序集中的所有类访问internal 成员,而 friend 允许您授予某个类完全访问恰好一个其他类)

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

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

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 ;))

严肃地说:内部不如朋友,但它确实可以完成工作.请记住,您很少会不通过 DLL 将代码分发给 3rd 方开发人员;因此,只要您和您的团队了解内部类及其用途,您就应该没问题.

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.

编辑 让我澄清一下朋友关键字如何破坏 OOP.

EDIT Let me clarify how the friend keyword undermines OOP.

私有和受保护的变量和方法可能是 OOP 中最重要的部分之一.对象可以保存只有它们可以使用的数据或逻辑的想法允许您编写独立于您的环境的功能实现 - 并且您的环境不能改变它不适合处理的状态信息.通过使用friend,您将两个类的实现耦合在一起 - 如果您只是耦合它们的接口,情况会更糟.

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天全站免登陆