string是什么类型
string是什么类型 我一直认为string是引用类型 因为string是一个类 但做克隆的时候则是值类型的形式
[解决办法]
string 本来是引用类型,但被和谐了,被强迫弄成值类型的方式来运作
[解决办法]
System.String
- C# code
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
[解决办法]
用法同值类型,但本身是引用类型
------解决方案--------------------
引用类型,你发现的是优化后的结果。
[解决办法]
string是引用类型,但它也有一些值类型的特征。
String传值还是传引用
C#的String声明是class String,当然是传引用。
不过,之所以有这个疑惑,多数是因为这个情况:
string a = "aaa";
string b = a;
b = "bbb";
或者是这么几行代码:
public void Swap(string s1, string s2)
{
string temp=s1;
s1=s2;
s2=temp;
}
这时候结果一打印,结果发现a的值还没有变,Swap也没有成功,这时候就会有幻觉:是不是没有传引用啊?
呵呵,string不会这么粗暴的打乱“声明为class就是传引用”这种规则的。
分析一下:
string a = "aaa"; //==> a----->new String("aaa")
string b = a; //==> b----->a, 传引用
b = "bbb"; //==> b----->new String("bbb"), 传引用,b指向了一个新的字符串,a并没有变。
Swap函数也是这样,比如说传了a, b进去(a="aaa", b="bbb"),
//s1----->a, s2----->b
string temp=s1;//temp----->s1----->a
s1=s2; //s1----->s2----->b;
s2=temp; //s2----->temp----->a
结果是,s1和s2确实是Swap了,但是这种结果并不会影响到a和b