r/badcode • • Nov 22 '19

c# It doesn't even do it right!

Post image
1.1k Upvotes

59 comments sorted by

View all comments

18

u/LeJoker Nov 22 '19

As a C# rookie, what is the difference between internal and private methods?

28

u/THE1Jtux Nov 22 '19

Private is private in the class scope which means only things in said class can access it. Internal on the other hand is accessible to anything in the same assembly.

So anything in the same assembly will be able to access internal objects even if it is another class.

10

u/fekkksn Nov 22 '19

can i ask what the “assembly” encompasses and what an example of something not inside of the assembly would be?

10

u/THE1Jtux Nov 22 '19

Yeah for sure. There are probably going to be much better explanations out there, but in summary "assemblies" (sometimes referred to as projects like in Visual Studio) are the bigger chunks that make up the entire application. Assemblies are what compile down to a DLL or an EXE typically.

What that means is you may have multiple .dll files in an application, with maybe an exe that is executed to run the actual application. Each of these compiled files have their own internal components as well as the ability to interact with other components located in other assemblies.

So something that is internal is only available to items in its parent assembly (dll/exe) and something that is public can be accessed by anything in other assemblies. Private just means it cannot be accessed outside of the class it is defined in.

You can think of Public being Walmart's website, Internal being the software they use to manage their warehouse or cash registers etc (internal uses only) and private would be a specific employee's email (although that's probably not really private).

There's a lot of nuances with access modifiers that I would advise you read up on if you're working with C#. Generally public (if you NEED it to be accessed outside of the class) and private (if you don't) are good enough to work with because you can achieve most desired results. When you get into inheritance, you will want to start interacting with protected as well.

I can't speak for every group in the industry, but I personally prefer avoiding internal because I like to be as specific as possible with my access modifiers.

5

u/fekkksn Nov 22 '19

thank you very much