C
C#3mo ago
tommy

✅ difference between internal and file access modifiers

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