C # Public, private, protected, internal, protected internal

Source: Internet
Author: User

C # Public, private, protected, internal, protected internal & (2010-09-22 13:33:45) Reprint Tags: Category: C #

In the C # language, there are five types of access modifiers: public, private, protected, internal, protected internal. The scope of action is as follows:
Access modifier description
Public access. Not subject to any restrictions.
Private access. This class is restricted to access by members, subclasses, and instances.
protected protected Access. This class and subclass access is limited, and instances cannot be accessed.
Internal internal access. Access is limited to this item and cannot be accessed by others.
protected internal internal protection access. Limited to this item or subclass access, other inaccessible
The editable and default modifiers for C # member types are as follows:
Member type default modifier can be modifier
Enum Public None
class private public, protected, internal, private,
protected internal
Interface Public None
struct private public, internal, private
Let me take a look at the scope of public, private, protected, internal, and protected internal in conjunction with an example.

 usingSystem;usingSystem.Collections.Generic;usingSystem.Text;namespaceAccessModifier { Public classaccessmodifierclass{ Public stringgetpublicstring () {return "Public String"; } protected stringgetprotectedstring () {return "Protected String";} Private stringgetprivatestring () {return "Private String"; } Internal stringgetinternalstring () {return "Internal String"; }protected Internal stringgetprotectedinternalstring () {return "Protected Internal String"; } voidAvailableaccessmodifier () { This. Getpublicstring ();  This. Getprivatestring (); This. Getinternalstring ();  This. Getprotectedinternalstring ();  This. Getprotectedstring (); } } Public classTestAccessModifierClass1 {voidAvailableaccessmodifier () {Accessmodifierclass Item=Newaccessmodifierclass (); item. Getpublicstring (); item. Getinternalstring (); Item. Getprotectedinternalstring (); } }  Public classTestaccessmodifierclass2:accessmodifierclass {voidAvailableaccessmodifier () {Accessmodifierclass Item=Newaccessmodifierclass (); item. Getpublicstring (); Item. Getinternalstring (); Item. Getprotectedinternalstring (); Base. Getprotectedstring ();}} }

Accessmodifierclass is our access modifier class, which has five access modifier methods, visible in the Accessmodifierclass class Availableaccessmodifier () method can access all methods.
The Availableaccessmodifier () method in the TestAccessModifierClass1 class can only access the public, internal, and protected internal methods.
The TestAccessModifierClass2 class inherits from the Accessmodifierclass class, so its Availableaccessmodifier () method can access the public,internal, protected and protected internal methods.

C # Public, private, protected, internal, protected internal (GO)

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.