简体   繁体   中英

What is meant by the word 'program' in the C# Language Specification Version 5 while introducing keyword 'internal'?

I am reading C# Language Specification Version 5.0 .

In Page 14, Section 1.6.2 Accesibility, for "internal" access modifier it says "Access limited to this program".

What is a surprise for me is that we can read this as "it is possible to access some stuff in a program from another program".

How is that possible? Can you give an example where one program accesses part of the other program? Eg an access to another program that we can prevent using the access modifier "internal".

A common use of internal access is in component-based development because it enables a group of components to cooperate in a private manner without being exposed to the rest of the application code. For example, a framework for building graphical user interfaces could provide Control and Form classes that cooperate by using members with internal access. Since these members are internal, they are not exposed to code that is using the framework.

check this link : https://msdn.microsoft.com/en-us/library/7c5ka91b.aspx

Internal access modifier prevents any member from being used from another assembly.

Consider a big application with a start project in solution which uses another Class Libraries. Creating an internal member anywhere in those Class Libraries will prevent them from being used in your start project, that is anywhere outside than in another members of declaring Class Library.

Let's say you have a console application and your application uses many other classes called models to map your data. If your application is big, then it is expected that you will have the model classes in a separate project alltogether for better code maintainability.

Now, if you keep the models as internal classes, even if you add reference of you model project to your console app, your app will not be able to find those classes and will give an compiler error like "Inacssible due to access modifiers".

The C# language specification is not written to be easy to read, it's written to be precise. As a result, some terms have meanings you might not understand without looking up the definition, even if the term in common everyday English has a different meaning.

Quoting the spec:

1.2 Program structure

The key organizational concepts in C# are programs , namespaces , types , members , and assemblies . C# programs consist of one or more source files. Programs declare types, which contain members and can be organized into namespaces. Classes and interfaces are examples of types. Fields, methods, properties, and events are examples of members. When C# programs are compiled, they are physically packaged into assemblies. Assemblies typically have the file extension .exe or .dll , depending on whether they implement applications or libraries .

From the perspective of the C# spec, a program is the source files that make up an assembly. It is not what typical users think of as a program. What typical users think of as a program, consists of multiple assemblies, each formed from what the language spec calls a program. Having multiple programs work together is normal, it's what happens even in Console.WriteLine("Hello, world!"); : your program contains the call, but the definition comes from another program.

If you are talking about a "program" then the basic example would be a simple DLL file. Only what is public is accessible from outside the DLL, while "internal" will only be accessible from within the DLL.

Each project in a Visual Studio solution is an assembly. When a class in one project is marked as public, all other projects can reach that class and work with it. If that class is marked with internal, only classes inside that project will be able to reach it and work with it. All other projects will not be able to work with it.

Why should you use it? mostly to let other programmers know they should not call this class directly. Eg you want the entities in your application to be created only via the factory class which returns only contracts (interfaces) (look for the factory design pattern). The factory will return an object of type Person entity (which is not accessible for you) but you will get a contract IPerson (which is accessible to you).

Other uses are when you publish an API for other companies or developers to use and you want to use some stuff but you don`t want to expose to them.

This is the basic idea for internal. I mostly saw my first example live in the field.

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