1

I read several topics, Googled many sites, read dozen of definitions about "internal" access modifier. But still confusing. I could not find exact definition of assembly.

Let's assume in Visual Studio I have Solution, in which I have Two projects -

ProjectA (NameSpaceA), ProjectB (NameSpaceB).
ProjectA has references of ProjectB.

Each project has 2 class (.cs) files
 - ProjectA (ClassA1.cs, ClassA2.cs)
 - ProjectB (ClassB1.cs, ClassB2.cs)

in ClassA1.cs file I have two classes: A1ClassOne, A1ClassTwo. In all other .cs files only one class per file.

Each class in Solution is separate class (there is no derived classes)

Question: If in A1ClassOne I have method with access modifier "internal", then from which classes it will be accessible? What is meant by assembly in above solution example? Whole solution? Or each project is different assembly (two assemblies in solution)? Or each class (.cs) file? What if there are several classes in one .cs file?

In other words I need solution-wise explanation of assembly.

Ped7g
  • 16,236
  • 3
  • 26
  • 63
Elshad Shabanov
  • 483
  • 10
  • 17

1 Answers1

1

internal means you can 'see' it (reference, derive, etc) from classes within the same assembly (.DLL, .EXE)

Question: If in A1ClassOne I have method with access modifier "internal", then from which classes it will be accessible?

From all classes that are within the same assembly as where that method is.

What is meant by assembly in above solution example? Whole solution? Or each project is different assembly (two assemblies in solution)? Or each class (.cs) file? What if there are several classes in one .cs file?

A .DLL is an assembly. And so is an .EXE Which usually corresponds to a project, yes.

ProjectA (NameSpaceA)
    - ref ProjectB
    - ClassA1.cs
        - A1ClassOne
            - internal Method1
        - A1ClassTwo
    - ClassA2.cs
ProjectB (NameSpaceB)
    - ClassB1.cs
    - ClassB2.cs

Assembly 'A' would be A1ClassOne, A1ClassTwo. Method1 can be accessed from them.

Assembly 'B' would be all classes defined in ClassB1 and ClassB2 files. They cannot access Method1.

hyankov
  • 4,049
  • 1
  • 29
  • 46
  • So, all classes in my project may access internal member of class? But what is the difference between internal and public, if I have only one project in my solution. Usually in most cases there is only one project in one solution. – Elshad Shabanov Mar 01 '17 at 06:33
  • The difference is that when you compile the DLL, someone could add it as a reference to their project later. Your 'internal' classes/methods won't be visible to them. The public ones would be. Imagine your DLL is a library you want to provide to others. Do you want to expose everything? – hyankov Mar 01 '17 at 06:34
  • Thank you for detailed explanation. I could not understand physical .exe assembly concept from point of view of source files. Now it's clear. – Elshad Shabanov Mar 01 '17 at 06:37
  • It's not about the source files, but the project type. Class Library and executables (console app, windows app, etc) are assemblies. – hyankov Mar 01 '17 at 06:39