Home > Backend Development > C++ > What Are the Default Access Modifiers for Classes, Members, and Other Entities in C#?

What Are the Default Access Modifiers for Classes, Members, and Other Entities in C#?

Mary-Kate Olsen
Release: 2025-01-29 04:01:08
Original
290 people have browsed it

What Are the Default Access Modifiers for Classes, Members, and Other Entities in C#?

The default access decoration in the c#

The default access modifier in C# follows one principle: the accessability setting of each member is "the most restricted access permissions you can declare for the member".

Specifically, the default access to different entities in C# is as follows:

Class (CLASSSES):
  • Method (Methods): internal
  • Member (field, attribute): private
  • Constructor (constructor): private
  • DELEGATES:
  • private interface (interfaces):
  • private For example, the following code represents an external class called
  • , which contains a nested class
  • and a private method : public
  • This code is equivalent to the following code with an explicit access to the modifier:

Outer One of the exceptions of this rule is when you define different accessibility for different parts of the attribute (usually setter). For example: Inner Foo()

As described in the C# 3.0 specification (section 3.5.1), the default access decoration will be different according to the context of the statement:
<code class="language-csharp">namespace MyCompany
{
    class Outer
    {
        void Foo() { }
        class Inner { }
    }
}</code>
Copy after login

Namespaces:
<code class="language-csharp">namespace MyCompany
{
    internal class Outer
    {
        private void Foo() { }
        private class Inner { }
    }
}</code>
Copy after login

Type (Types):

(in the compilation unit or name space)
<code class="language-csharp">public string Name
{
    get { ... }
    private set { ... } // 显式设置为 private
}</code>
Copy after login

Class Members:

  • Struct Members: public (because the structure is sealed)
  • Interface Members: internal
  • enumeration Members:
  • private
  • The nested type follows the accessible rules that contain a class or structure, and the default is private visibility.

The above is the detailed content of What Are the Default Access Modifiers for Classes, Members, and Other Entities in C#?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template