C# String 字符串一些关键理解
2018-08-30 23:02
543 查看
#1 :在.Net Framework中,字符总是表示成16位Unicode的代码
#2 :String 和string 其实是一样的只是表现形式上不同
#3 :string类型被视为基元类型,也就是编译器允许在源代码中直接使用字面值表示字符串,编译器将这些字符串放到模块的元数据中,并在运行时加载和引用它们。
il查看如下:
IL指令构造对象的新实例是newobj 上面的IL中并没有newobj IL为 ldstr 也就是CLR 用一种特殊的方式构造了string对象
#4:string 中的 + 操作符的实质
如果所有的字符串是字面值,则编译器在编译时连接它们,最终只有一个字符串"23",对于非字面值的字符串使用+操作符,连接则在运行时进行,对于运行时连接不要使用+操作符,这样会在堆上创建多个字符串对象,堆是需要垃圾回收的,对性能有影响,对于这种应使用stringbuilder
#5:字符串是不可变的
string对象一旦创建便不能更改
IL代码:
虽然string是个对象存放在堆上,但它并不像其他的应用对象一样对它的改变直接反应,通过上面的代码我们的可以发现CLR为每个字符串都创建了对象,这样做的好处:字符串不可变,则操作或访问字符串时不会发生线程同步问题,CLR可通过共享多个完全一致的string内容,这样减少了系统中字符串数量 从而节省内存。
#6 :安全字符串
对于一些敏感数据我们可以使用System.Security.SecureString 该类实现了IDisposable 使用完后可以进行销毁,防止数据的泄露。
#2 :String 和string 其实是一样的只是表现形式上不同
#3 :string类型被视为基元类型,也就是编译器允许在源代码中直接使用字面值表示字符串,编译器将这些字符串放到模块的元数据中,并在运行时加载和引用它们。
1 static void Main(string[] args) 2 { 3 string s = "2"; 4 Console.WriteLine(s); 5 Console.ReadLine(); 6 }
il查看如下:
1 .method private hidebysig static void Main(string[] args) cil managed 2 { 3 .entrypoint 4 // 代码大小 21 (0x15) 5 .maxstack 1 6 .locals init ([0] string s) 7 IL_0000: nop 8 IL_0001: ldstr "2" 9 IL_0006: stloc.0 10 IL_0007: ldloc.0 11 IL_0008: call void [mscorlib]System.Console::WriteLine(string) 12 IL_000d: nop 13 IL_000e: call string [mscorlib]System.Console::ReadLine() 14 IL_0013: pop 15 IL_0014: ret 16 } // end of method Program::Main
IL指令构造对象的新实例是newobj 上面的IL中并没有newobj IL为 ldstr 也就是CLR 用一种特殊的方式构造了string对象
#4:string 中的 + 操作符的实质
1 static void Main(string[] args) 2 { 3 string s = "2"+"3"; 4 Console.WriteLine(s); 5 Console.ReadLine(); 6 }
如果所有的字符串是字面值,则编译器在编译时连接它们,最终只有一个字符串"23",对于非字面值的字符串使用+操作符,连接则在运行时进行,对于运行时连接不要使用+操作符,这样会在堆上创建多个字符串对象,堆是需要垃圾回收的,对性能有影响,对于这种应使用stringbuilder
#5:字符串是不可变的
string对象一旦创建便不能更改
1 static void Main(string[] args) 2 { 3 string s = "2"; 4 Console.WriteLine(s); 5 ChangeStr(s); 6 Console.ReadLine(); 7 } 8 private static void ChangeStr(string s) 9 { 10 s = "1"; 11 } 12 2 13 2
IL代码:
.method private hidebysig static void ChangeStr(string s) cil managed { .method private hidebysig static void Main(string[] args) cil managed { .entrypoint // 代码大小 35 (0x23) .maxstack 1 .locals init ([0] string s) IL_0000: nop IL_0001: ldstr "2" IL_0006: stloc.0 IL_0007: ldloc.0 IL_0008: call void [mscorlib]System.Console::WriteLine(string) IL_000d: nop IL_000e: ldloc.0 IL_000f: call void ConsoleApp2.Program::ChangeStr(string) IL_0014: nop IL_0015: ldloc.0 IL_0016: call void [mscorlib]System.Console::WriteLine(string) IL_001b: nop IL_001c: call string [mscorlib]System.Console::ReadLine() IL_0021: pop IL_0022: ret } // end of method Program::Main // 代码大小 9 (0x9) .maxstack 8 IL_0000: nop IL_0001: ldstr "1" IL_0006: starg.s s IL_0008: ret } // end of method Program::ChangeStr
虽然string是个对象存放在堆上,但它并不像其他的应用对象一样对它的改变直接反应,通过上面的代码我们的可以发现CLR为每个字符串都创建了对象,这样做的好处:字符串不可变,则操作或访问字符串时不会发生线程同步问题,CLR可通过共享多个完全一致的string内容,这样减少了系统中字符串数量 从而节省内存。
#6 :安全字符串
对于一些敏感数据我们可以使用System.Security.SecureString 该类实现了IDisposable 使用完后可以进行销毁,防止数据的泄露。
相关文章推荐
- C#中字符串的一些常见函数,比如string.Compare()
- 对java String类型字符串的一些浅薄理解
- C#中,截取Html字符串的函数(参数可以是string、DataSet或者DataTable)
- C#基础系列(6)-- 第二部分 字符串相关 -- byte[]与string之间的转换(1)
- string.Format对C#字符串格式化
- C# serialport控件的一些理解
- 三分钟理解Java中字符串(String)的存储和赋值原理
- string.Format对C#字符串格式化
- 黑马程序员--C#中字符串的一些知识小结
- [No0000EC]C# 字符串(String)
- 浅析C#中的string字符串
- C#语言之“string格式的日期时间字符串转为DateTime类型”的方法
- C#中字符串的一些方法
- C# 判断字符串是不是int型 简单理解的代码
- c# String字符串相关
- C# 字符串 合并时 + 和 stringbulilder 的区别是什么?
- 理解:引用类型,string类型(字符串驻留),值类型
- c# 关于WCF 接口参数 为 xml 格式的 字符串(string) 的 数据 处理
- asp.net学习笔记——关于字符串的一些(c#)
- C# StringBuider,字符串拼接工具类