Home > Backend Development > C++ > What Are the Unexpected Default Access Modifiers in C#?

What Are the Unexpected Default Access Modifiers in C#?

DDD
Release: 2025-01-29 04:14:09
Original
982 people have browsed it

What Are the Unexpected Default Access Modifiers in C#?

The default access decoration in the C#

C# roy the default access to the class, methods and other members, and the protection level of its application may be unexpected.

The default access level

The default access modifier in C# is not an explicit statement, but depends on the context. Generally speaking, it awarded the most restricted access permissions

by .

For example, in the following code fragment:

The default access modifier is:

<code class="language-csharp">public class Outer
{
    void Foo() {}
    class Inner {}
}</code>
Copy after login

OUTER class: Internet (accessible access to the same program)

    FOO method: Private (accessable in the class)
  • Inner class: Private (accessible only in the outer class)
  • Attribute access

attribute is a special situation. Although the attribute itself may have public access permissions, the setter can be explicitly limited:

Here, name can be visited publicly, but the modification permissions are limited to the class itself.

Other descriptions
<code class="language-csharp">public string Name
{
    get { ... }
    private set { ... }
}</code>
Copy after login

Naming space has hidden public access rights. Naming space or the type in the compilation unit defaults to the Internet access permissions.

    Class members have Private access rights by default.
  • interface and lift member hidden formula is PUBLIC.
  • The nested type inherits the default access to the father type.

The above is the detailed content of What Are the Unexpected Default Access Modifiers 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template