当前位置: 移动技术网 > IT编程>开发语言>.net > C# Array数组是引用类型

C# Array数组是引用类型

2019年04月17日  | 移动技术网IT编程  | 我要评论

盐亭县政务网,cx870航班,色母粒

在c#中值类型都是由system.valuetype的直接派生类,system.valuetype本身又是直接从system.object派生的。派生的意思是‘利用继承机制,新的类可以从已有的类中生出来‘。简单点就是‘粑粑生娃’。有时是‘爷爷生娃‘例如:枚举都从system.enum抽象类派生,而后者又从system.valuetype派生。值类型的基类是:system.valuetype,而引用类型的基类是system.object

 1 using system;
 2 using system.collections.generic;
 3 using system.linq;
 4 using system.text;
 5 
 6 namespace testarraylist
 7 {
 8     class program
 9     {
10         static void main(string[] args)
11         {
12             //system.array
13             //1、数组[]特定类型、固定长度
14             //2、探讨system.array与数组的关系
15             //3、证明数组是引用类型
16             string[] str1 = new string[3];
17             system.array array =new string[3];
18             str1[0] = "a";
19             str1[1] = "b";
20             str1[2] = "c";
21 
22             array = str1;
23             console.writeline(str1[2]);
24             console.writeline(array.getvalue(2));
25 
26             string[] str2 = new string[3];
27             system.array array2 = new string[3];
28             string d ="d";
29             string e ="e";
30             string f ="f";
31             array2.setvalue(d,0);
32             array2.setvalue(e, 1);
33             array2.setvalue(f, 2);
34             //强制类型装换 装箱
35             str2 =(string[])array2;
36 
37             console.writeline(str2[2]);
38             console.writeline(array2.getvalue(2));
39             //通过isvaluetype方法判断
40             bool arraytype = str1.gettype().isvaluetype;
41             console.writeline(arraytype);
42 
43             type arryatypet = str1.gettype();
44             console.writeline(arryatypet.tostring());
45             //通过gettype方法得到类型
46             string arraytype2 = typeof(string[]).tostring();
47             console.writeline(arraytype2);
48             console.readkey();
49         }
50     }
51 }


通过对象彼此赋值发现system.array对string类型的数组可直接接受赋值,其实system.array是所有数组的‘粑粑’,具体的数组都是通过它隐式派生来的。运行结果如下

小结:数组是派生自system.valuetype的一种引用类型的数据结构

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网