0

I started c# tutorial to learn for my internship. I saw somethign interesting when it comes to classes. You can either have public class {class name} or class {classname}. I looked it up and people said it means that the class is visible to your assembly. But what is assembly? What does it mean to be inside vs outside your assembly?

Cleptus
  • 3,446
  • 4
  • 28
  • 34
edo101
  • 629
  • 6
  • 17
  • Public is a access modifier. Everything has a default access modifier, but it is customary to always explicitly specify the one you want. After all, defaults can change. – Christopher May 19 '20 at 04:37
  • An assemply is the container of your classes and your other types, it is usually a DLL or an EXE file. It does contain your code. If you separate your code between assemblies like the main executable (exe file) and libraries (the DLL's) then the acces modifier `public` comes into play. Do note that there are two types of modifiers: There are accessibility modifiers (like `public`, `private` or `internal`) and inheritance modifiers (like `protected`). – Cleptus May 19 '20 at 06:55
  • So if my class is internal, my other DLL's won't be able to see it then? @bradbury9 – edo101 May 19 '20 at 18:26
  • "_So if my class is internal, my other DLL's won't be able to see it then?_" That is correct – Cleptus May 20 '20 at 06:02

1 Answers1

0

In source terms, the assembly is your project. After compilation it'll be either an EXE or a DLL file.

Basically, if you are not creating a separate project representing a library for use in other projects, you don 't necessarily need to bother with that distinction for now.

Joey
  • 344,408
  • 85
  • 689
  • 683
  • So DLL would mean an assembly and having a public class would allow other stuff in my DLL to access the class? @Joey – edo101 May 19 '20 at 05:09
  • @edo101 What you describe is the "internal" access modifier. And it is quite possible that is the default one, applied to classes: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/access-modifiers – Christopher May 19 '20 at 05:19
  • But that is talking about modifiers for class members and methods not the classes themsevles. It skips over that just like many tutorials @Christopher – edo101 May 19 '20 at 05:27
  • @edo101: The very first screenful (basically the first two sections) talk about accessibility modifiers for types. If you skip to the members section, then that's not the fault of the article, though. – Joey May 19 '20 at 12:27
  • It talks about access modifiers but it doesn't go into what an assembly is and how public class is affected vs non public class. @Joey – edo101 May 19 '20 at 18:24
  • @edo101 **All types and variables have a access modifier**. Either a explicit one you gave, or a implicit one the Compiler added because you gave none explicit. Your question boils down to "What is the default access modifier for class types?" – Christopher May 20 '20 at 12:34
  • @Christopher: Actually for top-level types; nested ones have a different default ;-) – Joey May 22 '20 at 05:29
  • @Joey They still have a access modifier. And even a default one. Just a slightly different default one. – Christopher May 23 '20 at 02:58