简体   繁体   中英

C#: Question about protected and internal variables

i am new to C# and reading this

  • protected : Only derived types or members of the same type.
  • internal : Only code within the same assembly. Can also be code external to object as long as it is in the same assembly. (default for types)
  • protected internal : Either code from derived type or code in the same assembly. Combination of protected OR internal.

whats protected internal for? doesn't internal also allow derived types to access the variables?

and whats an assembly?

Assemblies

An assembly is the .dll or .exe file that you get from compiling your code. If you have multiple projects within Visual Studio, then they will compile to different assemblies.

See Assemblies on the MSDN for more information.

Protected Internal

protected internal means it can be accessed both from sub-classes and classes within the same assembly. It is a more visible access modifier than protected or internal alone. If you want to restrict class members to only derived classes within the same assembly, then you must mark the class itself as internal and its members as protected .

See Access Modifiers (C# Programming Guide) for more information.

"Doesn't internal also allow derived types to access the variables?"

No, not if they're in different assemblies. As assembly is essentially a CLR (common-language runtime) DLL or EXE. You can think of it roughly as "library."

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM