其他分享
首页 > 其他分享> > 4.值类型和引用类型

4.值类型和引用类型

作者:互联网

class Program
{
static void Main(string[] args)
{
//值类型,观察a的值
int a = 20;
int b = a;
b += 5;

        //引用类型,观察score1的值得变化
        int[] score1 = { 29, 79, 30, 12 };
        int[] score2 = score1;
        score2[0] = score1[3] + 100;
        for (int i = 0; i < score1.Length; i++)
        {
            Console.WriteLine(score1[i]);
        }
        //ref  和  out, ref是有进有出,out是只出不进。
        int val = 0;
        Method(ref val);
        // val is now 44
        int value;
        Method1(out value);
        // value is now 44


        Console.Read();
    }
    static void Method(ref int i)
    {
        i = 44;
    }

    static void Method1(out int i)
    {
        i = 44;
    }


    //得到结论:
    //1.值类型有:int,double等值类型      struct结构体    enum枚举
    //2.引用类型的变量在传递给新变量时,传递的是变量本身(引用、地址、指针),新变量并没有开辟新的空间。它只是指向了新的变量。
    //新变量改变了值,本质上改变的是“被引用变量”的值。
    //引用变量有:数组,对象,字符串(字符串是引用变量,但效果是和值类型一样),自定义类

}

标签:score1,变量,int,44,引用,类型,out
来源: https://www.cnblogs.com/cq752522131/p/14207991.html