C
C#3w ago
tommy

✅ difference between internal and file access modifiers

the content on Microsoft Learn is a bit difficult to understand.
26 Replies
Pobiega
Pobiega3w ago
- 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 code
tommy
tommy3w ago
so when you say project, do you mean a collection of files?
Pobiega
Pobiega3w ago
no I mean a project C# is project based
tommy
tommy3w ago
yeah we have to create a Console App in .NET
Pobiega
Pobiega3w ago
projects can depend on other projects, but you cant access things that are internal from other projects sure, that would be an executable project.
tommy
tommy3w ago
then how do you define a project?
Pobiega
Pobiega3w ago
No description
Pobiega
Pobiega3w ago
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 project
tommy
tommy3w ago
got it
Pobiega
Pobiega3w ago
Foo/
|-src/
|-Foo.Web/
|-Foo.Web.csproj
|-Foo.Domain/
|-Foo.Domain.csproj
|-test/
|-Foo.Web.Tests/
|-Foo.Web.Tests.csproj
|-Foo.Domain.Tests/
|-Foo.Domain.Tests.csproj
|-Foo.sln
Foo/
|-src/
|-Foo.Web/
|-Foo.Web.csproj
|-Foo.Domain/
|-Foo.Domain.csproj
|-test/
|-Foo.Web.Tests/
|-Foo.Web.Tests.csproj
|-Foo.Domain.Tests/
|-Foo.Domain.Tests.csproj
|-Foo.sln
as an example
tommy
tommy3w ago
hmm so this is a solution containing 4 projects?
Pobiega
Pobiega3w ago
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 projects
tommy
tommy3w ago
got it, so if we want to limit the access of a type to a specific project, I am gonna use internal
Pobiega
Pobiega3w ago
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
tommy
tommy3w ago
wow
Pobiega
Pobiega3w ago
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 one
tommy
tommy3w ago
and just making sure if I know things correctly, a dll file is a file compiled to IL
Pobiega
Pobiega3w ago
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
tommy
tommy3w ago
okay sprry i got a call inbetween
Pobiega
Pobiega3w ago
np
tommy
tommy3w ago
this was a very satisfying response thank you man
Pobiega
Pobiega3w ago
np
tommy
tommy3w ago
do you how to close this post?
Pobiega
Pobiega3w ago
/close
tommy
tommy3w ago
okay
Pobiega
Pobiega3w ago
then the bot takes care of it
Want results from more Discord servers?
Add your server