1、C# 数组是值类型还是引用类型,对数组有什么更深层次的理解?


一、数组是什么类型?

案例:

 

public class Program
{
    public static void Main(string[] args)
    {
        int[] intList =new int[3] { 1,2,3};
        bool type = intList.GetType().IsValueType;
        Console.WriteLine($"数组是否为值类型:{type}");
        bool itemType = intList[0].GetType().IsValueType;
        Console.WriteLine($"数组内的元素是否为值类型:{itemType}");
        ChangeIntList(ref intList);
        Console.WriteLine($"intList={intList[0]}, {intList[1]}, {intList[2]}");
        Console.Read();
    }
    private static void ChangeIntList(ref int[] intList)
    {
        intList[0] = 9;
        intList[1] = 8;
        intList[2] = 7;
    }
}

 

执行结果:

 

 

 结论:C#数组为引用类型。int[] 数组内的元素为值类型

 

 

二、对数组的理解衍生?

1、数组派生于 System.Array

2、int[] 是引用类型,但 int[] 内部的 int 元素是 值类型。且 值类型的 int 元素都位于托管堆上,不在栈上。

3、值类型数组和应用类型数组的初始化差异

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM