学习笔记值类型和引用类型比较

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TypeTest
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] ints = { 1, 2, 3, 4, 5 };//the array is refrenceType so after call SomeFunction the value will chnaged
            int i = 2;  //  int is valueType so  after call SomeFunction the value will not changed 
            TestObj obj = new TestObj(); //this is a object refrenceType after call SomeFunction the value will changed
            obj.age = 3;

            string str = "4"; //the string type is special refrenceType after call SomeFunction the value will not changed 
            Console.WriteLine(ints[0].ToString());
            Console.WriteLine(i.ToString());
            Console.WriteLine(obj.age.ToString());
            Console.WriteLine(str);

            SomeFunction(ints, ref i,obj,str);

            //after Call SomeFunction 
            Console.WriteLine(ints[0].ToString());
            Console.WriteLine(i.ToString());
            Console.WriteLine(obj.age.ToString());
            Console.WriteLine(str);
            Console.Read();

        }
        
        static void SomeFunction(int[] ints ,ref int i,TestObj obj,string str)
        {
            ints[0] = 100;
            i = 200;
            obj.age = 300;
            str = "400";
        }
    }

    class TestObj
    {
        public int  age = 1;
    }
}

原文地址:https://www.cnblogs.com/liubaolongcool/p/2179245.html