✅ difference between internal and file access modifiers
the content on Microsoft Learn is a bit difficult to understand.
26 Replies
- internal: Only code in the same assembly can access this type or member.
- file: Only code in the same file can access the type or member.
an "assembly" is essentially a project, or dll file
file is literally the same .cs file
the
file
modifier is almost exclusive used with source generation, its not a modifier you would use when writing your own codeso when you say project, do you mean a collection of files?
no I mean a project
C# is project based
yeah we have to create a Console App in .NET
projects can depend on other projects, but you cant access things that are
internal
from other projects
sure, that would be an executable project.then how do you define a project?
Calculator
and CalculatorLibrary
here are two different projects in the same solution
the top one is likely a WinForms project and is the "startup project", while the bottom one is a class library that is used by the other project
strictly speaking, a project is defined by the existance of a .csproj
file in a directory
anything under taht directory (including sub-directories) is considered part of that projectgot it
as an example
hmm
so this is a solution containing 4 projects?
correct
the
Foo.Web
project is likely the startup project, probably an ASP.NET application of some sort
Foo.Domain
is a normal class library, while the two test projects are... well, test projectsgot it, so if we want to limit the access of a type to a specific project, I am gonna use
internal
yeah exactly
then only code inside that project can use that type
there is a way to intentionally expose internals to another project, and its almost exclusively used for testing
wow
you can in
Foo.Domain.csproj
declare that Foo.Domain.Tests
should have access to internals
so its controlled by the declaring project, not the consuming oneand just making sure if I know things correctly, a dll file is a file compiled to IL
yeah, but thats mostly an implementation detail
and not all DLL files are compiled .NET projects, there might be native dlls too, in the case of a package like Sqlite
okay
sprry
i got a call inbetween
np
this was a very satisfying response
thank you man
np
do you how to close this post?
/close
okay
then the bot takes care of it