Elshad Shabanov
Elshad Shabanov

Reputation: 603

.Net assemblies and internal access modifier usage

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.

Upvotes: 1

Views: 354

Answers (1)

hyankov
hyankov

Reputation: 4130

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.

Upvotes: 1

Related Questions