programing tip

계획된 "개인 보호"C # 액세스 수정 자의 의미는 무엇입니까?

itbloger 2020. 6. 28. 19:14
반응형

계획된 "개인 보호"C # 액세스 수정 자의 의미는 무엇입니까?


GitHub 에 대한 Roslyn 문서의 일부로 C # 및 VB에 대해 계획된 언어 기능이있는 언어 기능 구현 상태 페이지가 있습니다.

머리를 감쌀 수 없었던 한 가지 기능은 private protected액세스 수정 자 였습니다 .

private protected string GetId() { … } 

C # 언어 디자인 노트 페이지에는 많은 새로운 기능을 설명하지만이 기능은 설명하지 않습니다.

에릭 리퍼 (Eric Lippert)는 다음과 같이 언급했다 .

오류가 수정자를 증가하는 제한 사항으로 생각하고 있습니다. 실제로 수정자는 항상 제한을 줄입니다. 사물은 기본적으로 "개인"입니다. 수정자를 추가해야만 제한이 줄어 듭니다.

의 의미는 private protected무엇입니까? 언제 사용할 수 있습니까?


1699 페이지의 De Bill Evjen과 Jay Glynn의 " Professional C # 2008 "에 따르면 :

개인 보호- "현재 어셈블리 내에서 파생 된 유형 만"

C ++ / CLI에는 클래스 및 구조 정의 및 사용 (C ++ / CLI)> 멤버 표시 기능과 유사한 기능이 있습니다 .

private protected-또는 protected private-구성원은 어셈블리 내부에서 보호되지만 어셈블리 외부에서는 비공개입니다.


Venn 다이어그램의 모든 액세스 수정자는 제한에서 무차별까지입니다.

private:
여기에 이미지 설명을 입력하십시오

private protected: -C # 7.2에 추가됨
여기에 이미지 설명을 입력하십시오

internal:
여기에 이미지 설명을 입력하십시오

protected:
여기에 이미지 설명을 입력하십시오

protected internal:
여기에 이미지 설명을 입력하십시오

public:
여기에 이미지 설명을 입력하십시오


이것은 접근성 수준이 다른 ( http://ashitani.jp/gv/ 로 만든 ) 그래프를 제공하기위한 것입니다 (이미지는 주석에 맞지 않습니다).

C # 액세스 레벨의 다이어그램 다이어그램

각 화살표는 "보다 제한적입니다"를 의미합니다.

The CLR names are Private, FamilyANDAssembly, Assembly, Family, FamilyORAssembly, Public.


Much later edit: It turned out this nice new access level (with a really poor name) was not eventually included in C# 6.0. It is supported only from C# 7.2 (and I see you updated your question "tags").


It's just a guess, but from a name you could possibly guess it's a more restricted version of protected, (or more relaxed version of private if you wish). And only reasonable variant of it is restricting protected behaviour to assembly.

Possible usage: then you want to have protected for internal implementation, but not for external uses (and you don't want sealing the class).

P.S. It always existed in CLR, but not in C#. It's a combination of protected and internal, quote:

CLR also supports “Family and assembly” access type. This means that the method is accessible from within the declaring type, nested and derived types but only if they’re declared in the same assembly. Well, apparently C# team didn’t think of this as a very useful feature so it’s not supported in this language.


"May be" only visible to subclasses that are in same assembly. This makes it a little restricted than protected.


See the spec for the "private protected" feature:

The intuitive meaning of private protected is “accessible within this assembly by types derived from the containing class”.

참고 : https://stackoverflow.com/questions/22856215/what-is-the-meaning-of-the-planned-private-protected-c-sharp-access-modifier

반응형