最近,正在简单地学习C#的一些知识。
C#是区分大小写的,但是我却发现C#中同时存在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,怎么实现的?
以下查询结果以及我的理解:
传值的情况 :Struct、Enumeration、Numeric(Integral/Floating/decimal)、bool
传引用的情况:class、Delegate、Interface
当使用操作符"="以及函数传参数的时候:
传值的结果是把原对象复制了一份,接收者指向原对象。
传引用的结果是直接让接收者指向原对象。
c、凉拌:)
2. String传值还是传引用
C#的String声明是class String,当然是传引用。
1
2
3
|
string
a =
"aaa"
;
string
b = a;
b =
"bbb"
;
|
或者是这么几行代码
1
2
3
4
5
6
|
public
void
Swap(
string
s1,
string
s2)
{
string
temp=s1;
s1=s2;
s2=temp;
}
|
1
2
3
|
string
a =
"aaa"
;
//==> a----->new String("aaa")
string
b = a;
//==> b----->a, 传引用
b =
"bbb"
;
//==> b----->new String("bbb"), 传引用,b指向了一个新的字符串,a并没有变。
|
1
2
3
4
|
//s1----->a, s2----->b
string
temp=s1;
//temp----->s1----->a
s1=s2;
//s1----->s2----->b;
s2=temp;
//s2----->temp----->a
|
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有什么区别?
4. String为什么是Immutable,怎么实现的?
immutable:对象一旦生成不可改变