C#中私有类的用途是什么 [英] what is the use of Private Classes in C#

查看:93
本文介绍了C#中私有类的用途是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我们要在C#中创建私有类?我们知道私有类不能成为基类,也不能从它们派生这些类.私有类的用途和目的是什么?

why we create private classes in c#? As we know that private class couldn't be make base class and we couldn't derived the classes from them.And what is the use and purpose of private class?

推荐答案

由于如果您要构建一个私有类的库,则您的库可以使用这些类,而使用您的库的任何人都将无法看到它们.这样可以隐藏您希望隐藏的特定于实现的细节.

If you are building a library will private classes, your library can use those classes while anyone using your library will not be able to even see them. This can hide implementation-specific details that you wish to conceal.

如果您要构建具有私有嵌套类的类,则包含类可以使用那些私有类,而其他任何人都无法看到它们.同样,您可以使用它来隐藏希望隐藏的特定于实现的细节.

If you are building a class that has private nested classes, the containing class can use those private classes without anyone else being able to see them. Again, you can use this to hide implementation-specific details that you wish to conceal.

如果您不隐藏特定于实现的详细信息(例如,通过公开私有类),则更改实现会变得更加困难,因为您无法确定库的用户尚未创建依赖于这些细节的依赖项类.但是,如果它们是私有的,则可以更改,替换或删除它们,而无需担心破坏外部依赖关系.

If you do not hide implementation-specific details (e.g. by making a private class public) then it becomes more difficult to change the implementation, since you can't be sure that users of your library haven't created dependencies on those classes. But if they are private, you can change them, replace them, or remove them, all without any worry of disrupting external dependencies.

这篇关于C#中私有类的用途是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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