15

What is the default access modifier of a class?

7 Answers 7

15

internal, if defined directly in the namespace (probably most classes):

Internal is the default if no access modifier is specified

For classes within other classes, the default is private, like class members:

The access level for class members and struct members, including nested classes and structs, is private by default.

Source: http://msdn.microsoft.com/en-us/library/ms173121.aspx

Sign up to request clarification or add additional context in comments.

Comments

8

by Default Internal is the access modifier of class

if it's not a nested class (for nested classes, it's private).
5

An enum has default modifier as public

A class has default modifiers as Internal . It can declare members (methods etc) with following access modifiers: public internal private protected internal

An interface has default modifier as public

A struct has default modifier as Internal and it can declare its members (methods etc) with following access modifiers: public internal private

A methods, fields, and properties has default access modifier as "Private" if no modifier is specified.

Comments

3

I believe it's internal.

Comments

1

Normally, the accessibility of a member is not greater than the accessibility of the type that contains it. However, a public member of an internal class might be accessible from outside the assembly if the member implements interface methods or overrides virtual methods that are defined in a public base class.

When a member of a class is a property, field, method, event, or delegate, and that member either is a type or has a type as a parameter or return value, the accessibility of the member cannot be greater than the type. For example, you cannot have a public method M that returns a class C unless C is also public. Likewise, you cannot have a protected property of type A if A is declared as private.

Comments

1

internal, except for for nested types in which case it is private

Comments

1

The default access modifier for a class is internal if it's defined within the same namespace. It is private if it's defined within another class.

Comments

Your Answer

Draft saved
Draft discarded

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.