1.  private / Private – 只有在类内部是可见的

protected / Protected – 只有在类内部或派生类中是可见的

internal / Friend – 只有在同一个工程中/程序集中是可见的

protected internal / Protected Friend – 只有在类内部、相同的工程/程序集当中的类和派生类中是可见的

public / Public – 全局可见的



 

代码
1 using System;
2  using System.Collections.Generic;
3  using System.Linq;
4  using System.Text;
5
6  namespace QuanXian
7 {
8
9 public class Class1
10 {
11 private int i;
12 internal int j;
13 public int k;
14 protected int l;
15 protected internal int m;
16
17 public void Test()
18 {
19 i = 1;
20 }
21 }
22
23 public class Class2 : Class1
24 {
25 public Class2()
26 {
27 }
28 public void TestMethod()
29 {
30 j = 3;
31 k = 4;
32 l = 5;
33 m = 6;
34 Class1 cd = new Class1();//jkm 说明对象不能访问私有和保护变量
35  
36 }
37 }
38
39 class Program
40 {
41 static void Main(string[] args)
42 {
43 Class1 cd = new Class1();//jkm
44   cd.j = 1;
45 cd.k = 1;
46 cd.m = 1;
47 //cd.l = 1;
48  
49
50 Class2 c2 = new Class2();//jkm
51   c2.j = 1;
52 c2.k = 1;
53 c2.m = 1;
54 //cd.l = 1;
55  
56 }
57 }
58 }
59  

 

Protected 只对派生类可见。

 

另一个project想引用此project(QuanXian)

1:add ref

2:using QuanXian;

代码
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 using QuanXian;
7
8 namespace Project2
9 {
10
11 class Class3 : Class1
12 {
13
14 public Class3()
15 {
16 //j = 3;
17 k = 4;
18 l = 5;
19 m = 6;
20
21
22 Class1 cd = new Class1();//jkm
23 //cd.j = 1;
24 cd.k = 1;
25 //cd.m = 1;
26 //cd.l = 1;
27
28 }
29 public void test2()
30 {
31
32 }
33 }
34
35
36 class Program
37 {
38
39
40 static void Main(string[] args)
41 {
42 }
43 }
44 }
45