日期:2014-05-18 浏览次数:20538 次
using System.Collections; using System.Collections.Generic; using System.Globalization; using System.Reflection; using System.Runtime.ConstrainedExecution; using System.Runtime.InteropServices; using System.Text; namespace System { // Summary: // Represents text as a series of Unicode characters. [Serializable] [ComVisible(true)] public sealed class String : IComparable, ICloneable, IConvertible, IComparable<string>, IEnumerable<char>, IEnumerable, IEquatable<string> { ...... } }
------解决方案--------------------
引用类型
------解决方案--------------------
string 是引用类型,但是,string与引用类型在常见的操作上有一些区别。例如,修改其中一个字符串,就会创建一个全新的string对象,而另一个字符串没有改变。
------解决方案--------------------
string 是String的别名
------解决方案--------------------
CLR via Cs中有一章介绍字符和字符串,下面是相关节选:
Strings Are Immutable
The most important thing to know about a String object is that it is immutable. That is, once
created, a string can never get longer, get shorter, or have any of its characters changed. Having immutable strings offers several benefits. First, it allows you to perform operations on a string
without actually changing the string:
if (s.ToUpperInvariant().SubString(10, 21).EndsWith("EXE")) {
...
}
Here, ToUpperInvariant returns a new string; it doesn't modify the characters of the string s.
SubString operates on the string returned by ToUpperInvariant and also returns a new
string, which is then examined by EndsWith. The two temporary strings created by ToUpper-
Invariant and SubString are not referenced for long by the application code, and the garbage
collector will reclaim their memory at the next collection. If you perform a lot of string manipulations,
you end up creating a lot of String objects on the heap, which causes more frequent
garbage collections, thus hurting your application's performance. To perform a lot of string
manipulations efficiently, use the StringBuilder class.
Having immutable strings also means that there are no thread synchronization issues when
manipulating or accessing a string. In addition, it's possible for the CLR to share multiple
identical String contents through a single String object. This can reduce the number of
strings in the system—thereby conserving memory usage-and it is what string interning
(discussed later in the chapter) is all about.
For performance reasons, the String type is tightly integrated with the CLR. Specifically, the
CLR knows the exact layout of the fields defined within the String type, and the CLR accesses
these fields directly. This performance and direct access come at a small development cost:
the String class is sealed. If you were able to define your own type, using String as a base
type, you could add your own fields, which would break the CLR's assumptions. In addition,
you could break some assumptions that the CLR team has made about String objects being
immutable.
------解决方案--------------------
或者参考这篇文章:
理解C#中的string类型
http://www.knowsky.com/3389.html
------解决方案--------------------
用法同值类型,但本身是引用类型
------解决方案--------------------