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

Source: Internet
Author: User

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.
The following code:
1:using System;
2:using System.Collections.Generic;
3:using System.Text;
4:
5:namespace AccessModifier
6: {
7:public class Accessmodifierclass
8: {
9:public string getpublicstring ()
10: {
11:return "Public String";
12:}
13:
14:protected string getprotectedstring ()
15: {
16:return "Protected String";
17:}
18:
19:private string getprivatestring ()
20: {
21:return "Private String";
22:}
23:
24:internal string getinternalstring ()
25: {
26:return "Internal String";
27:}
28:
29:
protected internal string getprotectedinternalstring ()
30: {
31:return "Protected Internal String";
32:}
33:
34:void Availableaccessmodifier ()
35: {
36:this. Getpublicstring ();
37:this. Getprivatestring ();
38:this. Getinternalstring ();
39:this. Getprotectedinternalstring ();
40:this. Getprotectedstring ();
41:}
42:}
43:
44:
public class TestAccessModifierClass1
45: {
46:void Availableaccessmodifier ()
47: {
48:accessmodifierclass item = new Accessmodifierclass ();
49:item. Getpublicstring ();
50:item. Getinternalstring ();
51:item. Getprotectedinternalstring ();
52:}
53:}
54:
55:public class Testaccessmodifierclass2:accessmodifierclass
56: {
57:void Availableaccessmodifier ()
58: {
59:accessmodifierclass item = new Accessmodifierclass ();
60:item. Getpublicstring ();
61:item. Getinternalstring ();
62:item. Getprotectedinternalstring ();
63:base. Getprotectedstring ();
64:}
65:}
}accessmodifierclass is our access modifier class, which has five access modifier methods, visible in the Accessmodifierclass class The 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 (reprint)

Related Article

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.