• C# string 特殊引用类型


    C#有string关键字,在翻译成.NET类时,它就是System.String.有了它,像字符串连接和字符串复制这样的操作就简单了.

    1.  string 是引用类型还是值类型

    引用类型操作

    当使用重载操作符”=”给string对象赋值时,string的对象是引用类型,它保留在堆上,而不是堆栈上.因此,当把一个字符串赋给另一个字符串时,会得到对内存中同一个字符串的两个引用.例如,修改其中一个字符串,就会创建一个全新的string对象(注意,这个过程发生在”=”中),而另一个字符串没有改变.考虑下面的代码:

    public class MyClass

    {

          public static void Main()

          {

            string str1 = "I am a number";

            string str2 = str1;

            Console.WriteLine("str1 = "+str1);

            Console.WriteLine("str2 = "+str2);

           

            str1 = "I am another number"; 

            Console.WriteLine("after str1 changed... str1 = "+str1);

            Console.WriteLine("after str1 changed... str2 = "+str2);

               Console.ReadLine();

          }   

    }

    Output :

    str1 = I am a number

    str2 = I am a number

    after str1 changed...str1 = I am another number

    after str1 changed...str2 = I am a number

    具有值类型特征的操作

    string有两种情况下的操作是具有值类型特征的:

    <!--[if !supportLists]-->1)        <!--[endif]-->在函数中传递string(比如函数参数是string型)时,传递的是地址,但却不能修改成员变量,原因是它重新又创建了一个全新的对象,和它想修改的那个成员变量非同一地址,所以看上去像是值类型;

    <!--[if !supportLists]-->2)        <!--[endif]-->str1 == str2 ,仅仅是比较了值,而非地址(是MS重写了==运算符所致).

    总结:

    string 到底是引用类型还是值类型   答:引用类型 .   只不过它在某此操作上会表现出值类型的特征.

    string类型的另外一个特殊性在于它是“不会变”的,每次操作string,都相当于新建了一个string对象.
    <!--[if !supportLineBreakNewLine]-->
    <!--[endif]-->

    2.  ”@”在string中的用法

           都知道如果要使用转义字符的话,需要在字符前加上”\”,而C#提供了一种新的机制,使用”@”.在”@”后的字符串都看作是原意,不会解释为转义字符串.并且以”@”开头的字符串支持回车换行的显示方式(见下例).不过会增加到字符串长度,不推荐过多使用.

    public class MyClass

    {

          public static void Main()

          {

            string str1 = @"HelloWorld!";

            string str2 = @"line1: Hello

            line2: World!";

            Console.WriteLine("str1 length = "+str1.Length);

            Console.WriteLine("str2 length = "+str2.Length);

            Console.ReadLine();

          }   

    }

    Output :

    str1 length = 11

    str2 length = 34

    3.  String 和 string 的区别:

           String是CLR(运行时库)的类型名字,而string是C#中的关键字.其实C#在编译时,会增加代码(下面列出的),将string转换成System.String.

    using string = System.String;

    using sbyte = System.SByte;

    using byte = System.Byte;

    using short = System.Int16;

    using ushort = System.UInt16;

    using int = System.Int32;

    using uint = System.UInt32;

    4.  ”@”的其它用法

           在 C#  规范中, ”@”可以作为标识符(类名、变量名、方法名等)的第一个字符,以允许C# 中保留关键字作为自己定义的标识符.

    public class MyClass

    {

          public static void Main()

          {

            @class c = new @class();

            c.@static();

            Console.ReadLine();

          }   

        public class @class

        {

            private int @bool;

            public void @static()

            {

                Console.WriteLine("I've been staticed...and @bool is "+this.@bool);

            }

            public @class()

            {

                this.@bool = 999;

            }

        }

    }


    <!--[if !supportLineBreakNewLine]-->
    <!--[endif]-->

    5.  等于null 和 等于””的区别

           string = null; //不分配内存

           string = “”;     //分配字节为0的内存

  • 相关阅读:
    go相关
    mac下使用vscode技巧
    mac下secureCRT的使用技巧
    python subprocess实时输出
    python中多级目录导入模块问题
    python的print与sys.stdout
    python中类相关笔记
    python中sys.stdout.flush()的作用
    nginx+uwsgi配置
    虚拟机的 基本配置
  • 原文地址:https://www.cnblogs.com/qixuejia/p/2966496.html
Copyright © 2020-2023  润新知