What are the access modifiers available in C#?
Public, internal, protected, protected internal or private.

What is the difference between "protected" and "internal"?
Protected means within the same inheritance chain, internal means within the same assembly.

What access modifiers can be used against non-nested classes in C#?
Public or internal - the same goes for Interfaces. Note that non-nested classes cannot be private since it makes no sense for such classes to be private.

What access keywords can be used against nested classes in C#?
Public, internal or private.

What is the default accessibility of classes in C#?
Internal is the default if no access modifier is specified for a class and also for interfaces.

What access modifiers can be used against class members in C#?
Public, internal, protected, protected internal or private.

What is the default accessibility of class members in C#?
Private. Same goes for struct members.

Can you add access modifiers to interface members?
NO. The point of an interface is to describe a contract between an implementing type and users of the interface. Outside callers aren't going to care and shouldn't have to care about implementation. If an interface is public then every part of that contract has to be public, otherwise it would mean one thing to friend/internal classes and a different thing to everything else. If you need default implementation then it's best to use an abstract class.

Can you add access modifiers to Enum members?
NO. Enum members are always public, and so no access modifiers can be applied.

Does the "protected internal" access modifer mean OR or AND?
It means protected **OR** internal. Not both.

What is an "inconsistent accessibility" error?
A derived class, or a member function in another class that returns a type cannot expose a type with greater visibility than that defined in the class itself. It is a sign of a faulty design if this situation happens and the compiler tells you so.

What access modifiers can be used against structs in C#?
Public or internal. This is the same as non-nested classes.

What access modifiers can be used against struct member functions in C#?
Public, private, or internal. Note that struct members cannot be declared as protected because structs do not support inheritance.

How is a "Friend Assembly" created?
You can enable specific other assemblies to access your internal types by using the InternalsVisibleToAttribute. This attribute is applied to the AssemblyInfo.cs file of the assembly granting the permission, and takes a parameter indicating the assembly receiving the permission. Note that private member functions and types will still remain inaccessible to the friend assembly.

What access modifiers can be used against instance constructors in C#?
Any of the 5. Public, internal, protected, protected internal or private.

What access modifiers can be used against class destructors in C#?
None. It's not your call dude! The garbage collector owns this puppy.

What access modifiers can be used against user-defined operators in C#?
Only public. It makes no sense to redefine an operator for a given class or struct and then have an access modifier other than public on it.

What access modifiers can be used against delegates in C#?
The public, protected, internal, and private modifiers control the accessibility of the delegate type. Depending on the context in which the delegate declaration occurs, some of these modifiers may not be permitted.

What is the default accessibility of delegate types in C#?
Internal.

Can namespaces have access modifiers?
No. All namespaces are implicitly public.

Bookmark and Share