What is the difference between "public class" and just "class"?

What is the difference between these 2 things?

Words like “public” and “private” are access modifiers, which control what other code is allowed to access the thing.

For classes, if you don’t specify which modifier you want, the default is “internal”, which means it can be accessed by other classes in the same assembly but not from different assemblies. (“Public” can be accessed from anywhere.)

2 Likes

It’s the access modifier, it defines what scopes have permission to access it (note this is just a compiler thing, you can always reflect out access even if it’s marked non-public).

public - accessible by anyone
private - accessible by no one accept the scope it’s defined in (classes can only be private if nested)
protected - accessible to the scope and inherited scopes (classes can only be protected if nested)
internal - accessible within this assembly
protected internal - combination of those
private protected - like protected, but restricted to assembly

If an access modifier isn’t defined each thing defaults to something specific to what it is.

class - defaults internal
members - (properties/fields/methods) default private

Access Modifiers - C# | Microsoft Learn.

3 Likes