C#中有关数组和string引用类型或值类型的判断

 

直接来一段测试代码

 class value_ref_type
    {
        public static void DEMO1()
        {
            double[] location = new double[2] { 1.256589, 489789 };
            double[] location_new;
            string str_1, str_2;

            Console.Out.WriteLine("ori location: x,{0};y{1}", location[0], location[1]);
            location_new = location;
            location[0] = 1.11111111111;
            str_1 = "weng";
            str_2 = str_1;
            str_1 = "jun";
            ChangeStrValue(str_2);

            Console.Out.WriteLine("out location: x,{0};y{1}", location_new[0], location_new[1]);
            Console.Out.WriteLine("out str ref: str1,{0};str2,{1}", str_1, str_2);
        }

        public static void ChangeStrValue(string str)
        {
            str = "tom";
        }
    }

结论:

(1)数组是引用类型的;

(2)string对象声明后尚未赋值的,string对象的默认值为null;

(3)string对象尚未赋值时虽然为null,但其和值类型的特性更为契合;在函数调用过程操作中,如果想在被调函数中修改主调函数的string值,最好参数加ref修饰。

 

posted @ 2018-05-14 10:44  wenglabs  阅读(650)  评论(0编辑  收藏  举报