Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the use case for the (C# 7.2) "private protected" modifier?

C# 7.2 introduces the private protected modifier.

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.

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.

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.

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?

like image 263
Jay Avatar asked Nov 22 '17 09:11

Jay


People also ask

What is C used for today?

It is used in developing an operating system. Operating systems such as Apple's OS X, Microsoft's Windows, and Symbian are developed using 'C' language. It is used for developing desktop as well as mobile phone's operating system. It is used for compiler production.

What is the use of switch case in C?

A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each switch case.

How do you write a case statement in C?

A general syntax of how switch-case is implemented in a 'C' program is as follows: switch( expression ) { case value-1: Block-1; Break; case value-2: Block-2; Break; case value-n: Block-n; Break; default: Block-1; Break; } Statement-x; The expression can be integer expression or a character expression.


2 Answers

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.

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() {
    }
}
like image 195
Evk Avatar answered Oct 11 '22 03:10

Evk


For two-word access modifiers I have this concept - the first accessor is related to another assembly, the second one to that assembly in which it was defined.

protected internal

  • protected in another assembly: accessible only in the child classes.

  • internal in the current assembly: accessible by everyone in the current assembly.

private protected

  • private in another assembly: is not accessible.
  • protected in the current assembly: accessible only in the child classes.
like image 31
Suren Srapyan Avatar answered Oct 11 '22 05:10

Suren Srapyan