C#编程 引用传递和值传递详细讲解
先来说下C#中的数据类型.分值类型和引用类型两大类.
值类型:直接存储数据的值,保存在内存中的stack(堆栈)中
引用类型:存储对值的引用,实际上存储的就是一个内存的地址.引用类型的保存分成两块,实际值保存在托管堆(heap)中.实际值的内存地址保存在stack中
当使用引用类型时先找到stack中的地址,再找到heap中的实际值.
也就是说保存引用类型时要用到stack和heap,但使用引用类型时我们实际上只用到stack中的值,然后通过这个值间接的访问heap中的值
C#预定义的简单类型,像int,float,bool,char都是值类型,另外enum(枚举),struct(结构)也是值类型
string,数组,自定义的class就都是引用类型了.其中的string是比较特殊的引用类型.C#给它增加个字符恒定的特性.
C#函数的参数如果不加ref,out这样的修饰符显式申明参数是通过引用传递外,默认都是值传递.
一、传递参数
既可以通过值也可以通过引用传递参数。通过引用传递参数允许函数成员(方法、属性、索引器、运算符和构造函数)更改参数的值,并保持该更改。
二、传递值类型参数
值类型变量直接包含其数据,这与引用类型变量不同,后者包含对其数据的引用。因此,向方法传递值类型变量意味着向方法传递变量的一个副本。方法内发生的对参数的更改对该变量中存储的原始数据无任何影响。如果希望所调用的方法更改参数的值,必须使用 ref 或 out 关键字通过引用传递该参数。为了简单起见,下面的示例使用 ref。
1. 通过值传递值类型:
class PassingValByVal{ static void SquareIt(int x) // The parameter x is passed by value. // Changes to x will not affect the original value of x. { x *= x; System.Console.WriteLine("The value inside the method: {0}", x); } static void Main() { int n = 5; System.Console.WriteLine("The value before calling the method: {0}", n); SquareIt(n); // Passing the variable by value. System.Console.WriteLine("The value after calling the method: {0}", n); }}
2.通过引用传递值类型
下面的示例除使用 ref 关键字传递参数以外,其余与上一示例相同。参数的值在调用方法后发生更改
class PassingValByRef{ static void SquareIt(ref int x) // The parameter x is passed by reference. // Changes to x will affect the original value of x. { x *= x; System.Console.WriteLine("The value inside the method: {0}", x); } static void Main() { int n = 5; System.Console.WriteLine("The value before calling the method: {0}", n); SquareIt(ref n); // Passing the variable by reference. System.Console.WriteLine("The value after calling the method: {0}", n); }}
本示例中,传递的不是 n 的值,而是对 n 的引用。参数 x 不是 int 类型,它是对 int 的引用(本例中为对 n 的引用)。因此,当在方法内对 x 求平方时,实际被求平方的是 x 所引用的项:n。
3. 交换值类型
static void SwapByRef(ref int x, ref int y){ int temp = x; x = y; y = temp;}
三、传递引用类型参数
1. 通过值传递引用类型
class PassingRefByVal { static void Change(int[] pArray) { pArray[0] = 888; // This change affects the original element. pArray = new int[5] {-3, -1, -2, -3, -4}; // This change is local. System.Console.WriteLine("Inside the method, the first element is: {0}", pArray[0]); } static void Main() { int[] arr = {1, 4, 5}; System.Console.WriteLine("Inside Main, before calling the method, the first element is: {0}", arr [0]); Change(arr); System.Console.WriteLine("Inside Main, after calling the method, the first element is: {0}", arr [0]); }}
2. 通过引用传递引用类型
class PassingRefByRef { static void Change(ref int[] pArray) { // Both of the following changes will affect the original variables: pArray[0] = 888; pArray = new int[5] {-3, -1, -2, -3, -4}; System.Console.WriteLine("Inside the method, the first element is: {0}", pArray[0]); } static void Main() { int[] arr = {1, 4, 5}; System.Console.WriteLine("Inside Main, before calling the method, the first element is: {0}", arr[0]); Change(ref arr); System.Console.WriteLine("Inside Main, after calling the method, the first element is: {0}", arr[0]); }}
3. 交换两个字符串
class SwappingStrings{ static void SwapStrings(ref string s1, ref string s2) // The string parameter is passed by reference. // Any changes on parameters will affect the original variables. { string temp = s1; s1 = s2; s2 = temp; System.Console.WriteLine("Inside the method: {0} {1}", s1, s2); } static void Main() { string str1 = "John"; string str2 = "Smith"; System.Console.WriteLine("Inside Main, before swapping: {0} {1}", str1, str2); SwapStrings(ref str1, ref str2); // Passing strings by reference System.Console.WriteLine("Inside Main, after swapping: {0} {1}", str1, str2); }}
四、引用类型的数据值传递(复本传递)
类的默认用MemberwiseClone 方法创建一个浅表副本,方法是创建一个新对象,然后将当前对象的非静态字段复制到该新对象。如果字段是值类型的,则对该字段执行逐位复制。如果字段是引用 类型,则复制引用但不复制引用的对象;因此,原始对象及其复本引用同一对象。深拷贝,即实现ICloneable接口.ICloneable可用于深拷贝 和浅拷贝。这些都是概念,但是需要我们理解:
class ClassA : ICloneable { public string str; public SubClass subclass; public ClassA() { str = "classA str"; subclass = new SubClass(); } //深复制,多层不可用MemberwiseClone()完整实现深复制 public object Clone() { // this.a = (string)this.a.Clone(); //this.b = (B)this.b.Clone(); var ne = new ClassA(); ne.str = this.str; ne.subclass = (SubClass)this.subclass.Clone(); //this.b的话还是没有成功 return ne; // return this.MemberwiseClone(); } } class SubClass : ICloneable { public string str; public SubClass() { this.str = "subclass str"; } //深复制,因为只一层,所以可以用MemberwiseClone()方法 public object Clone() { this.str = (string)this.str.Clone(); return this.MemberwiseClone(); }
评论