C
C#2mo ago
tommy

✅ difference between internal and file access modifiers

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