Default access permissions for C #

Source: Internet
Author: User

1. Classes in namespace, interfaces by default are internal types, and can also be displayed as public types and not allowed for other types of access.
2. Inside a class, properties and methods are private by default, and can be displayed as public, private, protected, internal, or protected internal, and other types of access.
3. The member variable cannot be defined in the interface, the method in the interface defaults to public access, but cannot display any access type defined.
4. Abstract classes must have more than one abstract method, the abstract method can be public, internal, protected, cannot be the private access type.

All members of the class, by default, are private.
C # uses a variety of modifiers to express the different properties of a class. There are five different restriction modifiers for classes based on their protection level C #:
Public can be arbitrarily accessed;
Protected can only be accessed by this class and its inheriting subclasses;
Internal can only be accessed by all classes within the group (Assembly), which is the logical and physical unit of a class being combined in the C # language, and its compiled file extension is often ". DLL "or". EXE ".
Protected internal is the only combination restriction modifier that can be accessed only by all classes in the group and by the inherited subclasses of those classes.
Private can only be accessed by this class.
If the class is not nested, the namespace or the class within the compilation unit has only public and internal two adornments.
The new modifier can only be used for nested classes, which represent shadowing of a type with the same name inherited from the parent class.
Override can only be used for nested classes, which represent overrides for types that inherit the same name as the parent class.

Abstract is used to decorate a class, indicating that the class can be used only as a parent class for inheritance, not object instantiation.
An abstract class can contain abstract members, but this is not required.
Abstract cannot be used in conjunction with new.
Here is the pseudo-code for abstract class usage:
Abstract class A {public abstract void F (); }
Abstract class B:a {public void G () {}}
Class C:b {public override void F () {//implementation of method F}}
Abstract Class A contains an abstract method F (), which cannot be instantiated. Class B inherits from Class A, which contains an instance method g (), but does not implement the abstract method F (), so it must still be declared as an abstract class.
Class C inherits from Class B, implements the class abstract method F (), and can then instantiate the object.
Sealed is used to decorate classes as sealed classes, preventing the class from being inherited. It is meaningless and forbidden to make an abstract and sealed modification to a class at the same time.

Default access permissions for C #

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.