zoukankan      html  css  js  c++  java
  • 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修饰。

  • 相关阅读:
    Men and women can't be 'just friends
    thin-provisioning-tools
    自签名证书
    sqlite manager
    python -m SimpleHTTPServer 80801
    rsa or dsa?
    sl4a
    mtp
    sl4a
    基站记录仪是个啥?
  • 原文地址:https://www.cnblogs.com/arxive/p/9034932.html
Copyright © 2011-2022 走看看