Jul 20, 2015

What are the Default Access Modifiers in C#?

Class and Struct Accessibility
Internal is the default if no access modifier is specified.



Class and Struct Member Accessibility
Class members (including nested classes and structs) can be declared with any of the five types of access. Struct members cannot be declared as protected because structs do not support inheritance.

The accessibility of a member can never be greater than the accessibility of its containing type


Destructors cannot have accessibility modifiers.

other Types
Interfaces declared directly with a namespace can be declared as public or internal and like classes and structs, interfaces default to internal access.

Enumeration members are always public, and no access modifiers can be applied.
By default, delegates have internal access.

So for example:
namespace MyCompany
{
    class Outer
    {
        void Foo() {}
        class Inner {}
    }
}
is equivalent to
namespace MyCompany
{
    internal class Outer
    {
        private void Foo() {}
        private class Inner {}
    }
}

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. - See more at: http://www.dotnetfunda.com/interviews/show/425/default-access-modifiers-in-csharp#sthash.yd1k2NG3.dpuf


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. - See more at: http://www.dotnetfunda.com/interviews/show/425/default-access-modifiers-in-csharp#sthash.TdmzsOtf.dpuf

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. - See more at: http://www.dotnetfunda.com/interviews/show/425/default-access-modifiers-in-csharp#sthash.TdmzsOtf.dpuf

No comments:

Post a Comment