C# String与string的区别 以及 C# Swap string

来源:互联网 发布:linux 删除文件 api 编辑:程序博客网 时间:2024/06/05 14:12

最近,正在简单地学习C#的一些知识。

C#是区分大小写的,但是我却发现C#中同时存在String与string,于是我很困惑,于是我上网搜索了一下,于是我了解了一些小知识。

MSDN中对string的说明:string is an alias for String in the .NET Framework。string是String的别名而已,string是c#中的类,String是Framework的类,C# string 映射为 Framework的 String。如果用string,编译器会把它编译成String,所以如果直接用String就可以让编译器少做一点点工作。

如果使用C#,建议使用string,比较符合规范 。 string始终代表 System.String(1.x) 或 ::System.String(2.0) ,String只有在前面有using System;的时候并且当前命名空间中没有名为String的类型(class、struct、delegate、enum)的时候才代表System.String。

string是关键字,String不是,也就是说string不能作为类、结构、枚举、字段、变量、方法、属性的名称,而String可以。

今天突然用到C#的string,突然想起来一直没弄明白string和String有什么区别,查了一下电子书、MSDN以及百度和Google,更进一步看看String的一些问题。


问题:

1. C#到底是什么时候传引用?什么时候传值?
2. String传值还是传引用
3. string和String有什么区别?
4. String为什么是Immutable,怎么实现的?

以下查询结果以及我的理解:

1. C#到底是什么时候传引用?什么时候传值?
传值的情况 :Struct、Enumeration、Numeric(Integral/Floating/decimal)、bool
传引用的情况:class、Delegate、Interface

当使用操作符"="以及函数传参数的时候:
 传值的结果是把原对象复制了一份,接收者指向原对象。
 传引用的结果是直接让接收者指向原对象。
有人说,我硬要把值当引用传怎么办?
a、用ref关键字
b、用数组,数组是class

c、凉拌:)



2. String传值还是传引用
C#的String声明是class String,当然是传引用。
不过,之所以有这个疑惑,多数是因为这个情况:
?
1
2
3
stringa = "aaa";
stringb = a;
b = "bbb";

或者是这么几行代码

?
1
2
3
4
5
6
publicvoid Swap(strings1, strings2)
{
 stringtemp=s1;
 s1=s2;
 s2=temp;
}

这时候结果一打印,结果发现a的值还没有变,Swap也没有成功,这时候就会有幻觉:是不是没有传引用啊?
呵呵,string不会这么粗暴的打乱“声明为class就是传引用”这种规则的。
分析一下:
?
1
2
3
stringa = "aaa";//==> a----->new String("aaa")
stringb = a; //==> b----->a, 传引用
b = "bbb";//==> b----->new String("bbb"), 传引用,b指向了一个新的字符串,a并没有变。


Swap函数也是这样,比如说传了a, b进去(a="aaa", b="bbb"),
?
1
2
3
4
//s1----->a, s2----->b
stringtemp=s1;//temp----->s1----->a
s1=s2;//s1----->s2----->b;
s2=temp;//s2----->temp----->a


结果是,s1和s2确实是Swap了,但是这种结果并不会影响到a和b

原因是因为: http://stackoverflow.com/questions/8604384/why-do-i-need-to-pass-strings-by-reference-to-my-swap-function 

Yes, string is a reference type and this

void swap(string first, string second)

passes references to the string objects to the function. But string is immutable so it is not possible for the swap function to change the objects through these references. For strings, the only way to implement a swap function is to use the ref keyword to pass the references by reference so the references can be swapped.

OTOH, if you have a mutable class, you can write a swap function without using the ref keyword:

class Foo{    public int Bar { get; set; }}static void Swap(Foo first, Foo second){    var temp = first.Bar;    first.Bar = second.Bar;    second.Bar = temp;}Foo foo1 = new Foo { Bar = 1 };Foo foo1Copy = foo1;Foo foo2 = new Foo { Bar = 2 };Swap(foo1, foo2);

But note, that after the swap, foo1Copy.Bar == 2, since the object referenced by foo1 and foo1Copy was modified.


You are confusing two different kinds of references. Strings are references to instances. Your swap function takes references to variables.

Think about it this way. You have two books, "A Christmas Carol" and "Pickwick Papers". They're books; they've got pages and text and whatnot.

You have ten pieces of paper. Five says "A Christmas Carol". Five says "Pickwick Papers". The papers are references to the books. They're not the books.

You have ten drawers labelled A, B, C, D, E, F, G, H, I, J. You put the papers that says "A Christmas Carol" into drawer A through E, and the papers that says "Pickwick Papers" into drawers F through J.

Now you want to swap the contents of drawers E and I. What information do you have to give to the swapper? You can't tell the swapper "Swap references to Christmas Carol for Pickwick Papers", because that would change all ten drawers, not just E and I. The information you have to give to the swapper is "E" and "I". You have to pass reference to two variables; the fact that the variables themselves contain a reference to a book is irrelevant.

Your swapper has three drawers of its own, labelled first, second and temp. It takes two piece of paper. One says "E", and that goes in the drawer labelled "first". One says "I" and that goes in the drawer labelled "second".

The swapper looks in "first" and finds a paper that says "E". It looks in "E" and finds a paper that says "Christmas Carol". It makes a photocopy of that paper and puts the copy in "temp"... and you see how this goes from here I hope.




3. string和String有什么区别?
MSDN中对string的说明:string is an alias for String in the .NET Framework
呵呵string是String的别名而已,都是一家。

4. String为什么是Immutable,怎么实现的?
immutable:对象一旦生成不可改变
关于怎么实现的,在明白了问题2之后很好办,只要不提供任何修改自己成员变量的方法就可以了。顺便声明为sealed,防止不清楚的后来者违反规定:)
String每个看似修改了成员变量的方法,事实上都返回了一个新的String。
比如String.Replace函数,事实上并没有改变原来的串,这也是为什么只有让str = str.Replace(   foo, bar   )才真正完成替换的原因。
0 0
原创粉丝点击