(C#7.2)“私有保护”的用例是什么?修饰符? [英] What is the use case for the (C# 7.2) "private protected" modifier?

查看:78
本文介绍了(C#7.2)“私有保护”的用例是什么?修饰符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C#7.2引入了受保护的私有修饰符

我一直保护对具有属性的字段的访问,允许通过Get / Set方法进行访问,因为我通常不希望对象的内部状态被任何东西修改除了我自己的班级。

I've always protected access to fields with properties, allowing access via the Get/Set methods as I typically don't want the internal state of my object modified by anything other than my own class.

我试图理解为什么C#语言团队添加了此功能。在Google上进行广泛搜索之后,阅读并观看了最新消息(我已经看过新闻稿详细信息 Mads Torgerson的视频),但我仍然不明智。

I'm trying to understand why the C# language team have added this feature. After an extensive search on google, and reading and watching the 'what's new' media (I've watched the press release, details and video by Mads Torgerson), I am still none the wiser.

对我来说,这似乎允许开发人员违反了Liskov替换原则,但这可能是因为我不了解为什么现在存在此功能。

To me, this appears to allow a developer to break the Liskov Substitution principle, but this may be because I do not understand why this feature now exists.

我了解如何使用它,而不是为什么-请可以有人提供了真实的用法示例,而不是MSDN文档中人为的示例?

I understand how it can be used, just not why - please can someone provide a real-world usage example rather than the contrived one in the MSDN documents?

推荐答案

在C#7.2之前,我们有受保护的内部修饰符。这实际上意味着受保护的OR内部,即-成员 A 可以被子类以及当前程序集中的任何类访问,即使该类不是 A (因此放宽了受保护所隐含的限制)。

Before C# 7.2 we had protected internal modifier. This really means protected OR internal, that is - member A is accessible to child classes and also to any class in the current assembly, even if that class is not child of class A (so restriction implied by "protected" is relaxed).

私有受保护的真正含义是受保护的和内部的。即-成员仅可用于同一程序集中的子类,而不能用于外部程序集的子类(因此,保护所隐含的限制会变得更加严格-变得更加严格)。如果您在程序集中建立类的层次结构并且不希望其他程序集中的任何子类访问该层次结构的某些部分,则这很有用。

private protected really means protected AND internal. That is - member is accessible only to child classes which are in the same assembly, but not to child classes which are outside assembly (so restriction implied by "protected" is narrowed - becomes even more restrictive). That is useful if you build hierarchy of classes in your assembly and do not want any child classes from other assemblies to access certain parts of that hierarchy.

我们可以举个例子: 乔恩·斯基特(Jon Skeet)提供了在注释中的使用情况。假设您有课程

We can take example that Jon Skeet provided in comments. Suppose you have class

public class MyClass {

}

并且您只希望在当前程序集中从其继承,而不希望直接实例化该类,除非从此内部实例化类层次结构。

And you want to be able to inherit from it only in current assembly, but do not want to allow to instantiate this class directly except from within this class hierarchy.

仅在当前程序集中进行继承可以使用内部构造函数

Inheriting only within the current assembly may be achieved with internal constructor

public class MyClass {
    internal MyClass() {
    }
}

可以使用受保护的构造函数来防止直接实例化(当前类层次除外):

Preventing direct instantiation except withing current class hierarchy may be achieved with protected constructor:

public class MyClass {
    protected MyClass() {
    }
}

两者-您都需要受私有保护的构造函数:

And to get both - you need private protected constructor:

public class MyClass {
    private protected MyClass() {
    }
}

这篇关于(C#7.2)“私有保护”的用例是什么?修饰符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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