C # type basis

C # type basis

introduction

The purpose of this paper is about the beginning of the design mode Prototype (Prototype) mode, but if you want to understand more clearly this mode, you need to understand the object clone (Object Clone), Clone fact, that is the object replication. Copy divided into a shallow copy (Shallow Copy) and deep copy (Deep Copy), shallow copy and a deep copy and how to copy a reference type is a member to the division. This in turn leads to the basics reference types and value type, and the other related objects sentence, packing, unpacking and the like.

So I'm just starting a new one, from the most basic type of start up from the bottom to write. I just want to express understanding of this topic out, first, a summary and review, the second is the exchange of experience, perhaps there where I understand there are deviations, correct me hope. If the content is too simple front foundation for you, you can jump to read.

Value types and reference types

We first briefly recall in C # type system. C # in total are divided into two types, one is the type of value (Value Type), one is a reference type (Reference Type). Value types and reference types based on their computer's memory is how the assigned to the division. Structure and value types include enum, reference types including classes, interfaces, delegates, and the like. There is a special value type, called the simple types (Simple Type), such as byte, int, etc., which is actually a simple type of alias FCL type library, such as an int type declaration, a statement actually System.Int32 structure type. Therefore, the operation defined in Int32 type, can be used in the int type, such as "123.Equals (2)".

All value types are implicitly inherit from type System.ValueType (note System.ValueType itself is a class type), and all references System.ValueType types are inherited from the base class System.Object. You can not explicitly allow the structure to inherit a class, because C # does not support multiple inheritance, while the structure has been implicitly inherited from ValueType.

NOTE : Stack (Stack) is a LIFO data structure in memory, the variables are allocated on the stack up operation. Heap (heap) is an instance of type (object) memory area allocated space on the heap to create an object, the object will be passed to variable address on the stack (called the turn variable pointing to the object, or a variable reference to this Object).

1. Value Type

When declare a value type variable (Variable), the variable itself contains all types of field values, the variable will be allocated on the thread stack (Thread Stack).

If we have such a value type, which represents the point on the line:

public struct ValPoint {
    public int x;

    public ValPoint(int x) {
       this.x = x;
    }
}

When we wrote the declaration of such a variable in the program:

ValPoint vPoint1;

The actual effect produced is declared vPoint1 variable, the variable itself contains all types of field values ​​(that is, all the data you want).

NOTE : If the observed MSIL code will be found at this time variable has not been pushed on the stack, as .maxstack (top of stack) is zero. And I do not see the command stack, indicating that only the variable operation, will be pushed onto the stack.

Because the variable already contains all types of field values, so now you have it can be operated (operating variables, in fact, a series of push, pop operation).

= 10 vPoint1.x;
Console.WriteLine (vPoint.x); // Output 10

NOTE : If vPoint1 is a reference type (such as class), the runtime will throw a NullReferenceException exception. Because vPoint is a value type, there is no reference, so never throw NullReferenceException.

If you do not vPoint.x assign, direct write Console.WriteLine (vPoint.x), a compile error occurs: the use of local variables unassigned. This error is due to a constraint on .Net: must be initialized before all of the elements to use. For example, such a statement would trigger this error:

int i;
Console.WriteLine(i);

We can solve this problem in such a way: the compiler implicitly creates a no-argument constructor for type structure. Structural members will in this constructor initializes all members are value types or equivalent given value (for type Char) 00, all the reference type is assigned a null value. (Thus, Struct not self-declared type constructor with no arguments). So, we can go to create a ValPoint type variable in the constructor implicitly declared:

= New new ValPoint vPoint1 ValPoint ();
Console.WriteLine (vPoint.x); // output 0

We code above by the expression of a "=" split into two parts separated of view:

  • Left ValPoint vPoint1, create a variable of type vPoint ValPoint on the stack, all members of the structure were not assigned. Before new ValPoint (), the vPoint on the stack.
  • The right new ValPoint (), new operator does not allocate memory, it simply calls the default constructor ValPoint structure, to initialize all the fields vPoint structure according to the constructor.

Note that the above sentence, new new operator does not allocate memory, only the default constructor call ValPoint structure to initialize all the fields of vPoint. What if I do this, how to explain it?

Console.WriteLine ((new ValPoint ()) x.); // normal, the output 0

In this case, it will create a temporary variable, then use the default constructor structure of this temporary variables are initialized. I know I was not persuasive, so we look at the MS IL code in order to save space, I have only a partial excerpt:

.locals init ([0] valuetype Prototype.ValPoint CS $ 0 $ 0000) // declare temporary variables
IL_0000: nop
IL_0001: ldloca.s CS $ 0 $ 0000 // temporary variables will push
IL_0003: initobj Prototype.ValPoint // initialize this variable

For ValPoint vPoint = new ValPoint (); case, MSIL code which is:

.locals init ([0] valuetype Prototype.ValPoint vPoint) // declare vPoint
IL_0000: nop
IL_0001: ldloca.s vPoint // will vPoint push
IL_0003: initobj Prototype.ValPoint // initialize this variable using initobj

So when we use a custom constructor, ValPoint vPoint = new ValPoint (10), then what will happen? The following code, we can see that, actually using the call instruction (Instruction) calling the constructor our custom, 10 and passed to the parameter list.

the init .locals ([0] ValueType Prototype.ValPoint vPoint)
IL_0000: NOP
IL_0001: // ldloca.s vPoint will push vPoint
IL_0003: ldc.i4.s 10 // 10 Yazhan
// call the constructor, passing parameters
IL_0005: call instance void Prototype.ValPoint :: ctor   (int32).

For the above MSIL code is not clear it does not matter, sometimes know the result has been good enough. About MSIL code, the time I will translate some good articles for you.

2. The reference type

When declaring a reference type variable, the reference type is assigned to a variable on the stack, this variable will be used to store the memory address in the heap of a reference instance of the type, the variable itself does not contain data objects. At this time, if only declare such a variable, because the instance type of the heap has not been created, and therefore, the variable value is null, meaning not point to any type instance (object on the heap). For the type declaration of variable, this variable can be used to limit the type of save.

If we have such a class, it still represents the point on the line:

public class RefPoint {
    public int x;

    public RefPoint(int x) {
       this.x = x;
    }
    public RefPoint() {}
}

When we just wrote a declaration statement:

RefPoint rPoint1;

FIG down its effect is the same, only create a does not contain any data on the stack, does not point to any object (not re-create the object contains the address of the heap) variables.

When we use the new operator:

rPoint1= new RefPoint(1);

Such a thing would happen:

  1. Create an instance (Instance) type (Type) a reference to an object or call (Object) in the application heap (Heap) on, and allocate memory for it.
  2. Automatically passed to the constructor a reference to the instance. (Because of this, you can use this in the constructor to access this instance.)
  3. Call the type constructor.
  4. Returns the instance reference (memory address), assigned to rPoint variables.

3. With regard to simple types

Many articles and books about such problems at the time, always like to use an int type as a value type and an Object type for illustration as a reference type. Self herein as a class definition and the structure and reference types for the values ​​are described. This is because the simple types (such as int) CLR implements some of the behaviors that make us misunderstand some operations.

For example, if we want to compare two int type are equal, we would generally like this:

int i = 3;
int j = 3;
if(i==j) Console.WriteLine("i equals to j");

However, the value of a custom type, such as the structure, can not be used "==" to determine whether they are equal, on the need to use variable Equals () method to complete.

As another example, we know that string is a reference type, and we compare them for equality, generally do so:

string a = "123456"; string b = "123456";
if(a == b) Console.WriteLine("a Equals to b");

In fact, later we will see, when using the "==" to compare the reference type variable when the comparison is whether they point to the same object on the heap. The above a, b points of the object are clearly different, but the same value contained object, therefore visible, for string types, the CLR actually comparing their values ​​are compared, rather than a reference.

In order to avoid confusion caused by these above object and other parts will be used from the sentence structure and the class definition will be described separately.

Boxing and unboxing

This part of deep or shallow, this article only briefly for a review. Briefly, a boxed value type is converted to an equivalent of the reference type. It's this process is divided into steps:

  1. Heap newly generated object (comprising the object data, the object itself has no name) memory allocation.
  2. The value type variable value on the stack to copy the object on the heap.
  3. The heap objects created to return the address of a reference type variable (from a programmer perspective, the name of the variable if the name of the same object on the heap).

When we run this code:

int i = 1;
Object boxed = i;
Console.WriteLine("Boxed Point: " + boxed);

FIG effect is this:

MSIL code like this:

.method private hidebysig static void  Main(string[] args) cil managed
{
  .entrypoint
  // 代码大小       19 (0x13)
  .maxstack  1                   // 最高栈数是1,装箱操作后i会出栈
  .locals init ([0] int32 i,     // 声明变量 i(第1个变量,索引为0)
           [1] object boxed)          // 声明变量 boxed (第2个变量,索引为1)
  IL_0000:  nop
  IL_0001:  ldc.i4.s   10         //#1 将10压栈
  IL_0003:  stloc.0                  //#2 10出栈,将值赋给 i
  IL_0004:  ldloc.0                  //#3 将i压栈
  IL_0005:  box   [mscorlib]System.Int32   //#4 i出栈,对i装箱(复制值到堆,返回地址)
  IL_000a:  stloc.1           //#5 将返回值赋给变量 boxed
  IL_000b:  ldloc.1           // 将 boxed 压栈
// 调用WriteLine()方法
  IL_000c:  call       void [mscorlib]System.Console::WriteLine(object)
  IL_0011:  nop
  IL_0012:  ret
} // end of method Program::Main

而拆箱则是将一个 已装箱的引用类型 转换为值类型:

int i = 1;
Object boxed = i;
int j;
j = (int)boxed;          // 显示声明 拆箱后的类型
Console.WriteLine("UnBoxed Point: " + j);

需要注意的是:UnBox 操作需要显示声明拆箱后转换的类型。它分为两步来完成:

  1. 获取已装箱的对象的地址。
  2. 将值从堆上的对象中拷贝到堆栈上的值变量中。

对象判等

因为我们要提到对象克隆(复制),那么,我们应该有办法知道复制前后的两个对象是否相等。所以,在进行下面的章节前,我们有必要先了解如何进行对象判等。

NOTE:有机会较深入地研究这部分内容,需要感谢 微软的开源 以及 VS2008 的FCL调试功能。关于如何调试 FCL 代码,请参考 Configuring Visual Studio to Debug .NET Framework Source Code

我们先定义用作范例的两个类型,它们代表直线上的一点,唯一区别是一个是引用类型class,一个是值类型struct:

public class RefPoint {      // 定义一个引用类型
    public int x;
    public RefPoint(int x) {
       this.x = x;
    }
}

public struct ValPoint { // 定义一个值类型
    public int x;
    public ValPoint(int x) {
       this.x = x;
    }
}

1.引用类型判等

我们先进行引用类型对象的判等,我们知道在System.Object基类型中,定义了实例方法Equals(object obj),静态方法 Equals(object objA, object objB),静态方法 ReferenceEquals(object objA, object objB) 来进行对象的判等。

我们先看看这三个方法,注意我在代码中用 #number 标识的地方,后文中我会直接引用:

public static bool ReferenceEquals (Object objA, Object objB)
{
     return objA == objB;     // #1
}
 
public virtual bool Equals(Object obj)
{
    return InternalEquals(this, obj);    // #2
}

public static bool Equals(Object objA, Object objB) {
     if (objA==objB) {        // #3
         return true;
     }

     if (objA==null || objB==null) {
         return false;
     }

     return objA.Equals(objB); // #4
}

我们先看ReferenceEquals(object objA, object objB)方法,它实际上简单地返回 objA == objB,所以,在后文中,除非必要,我们统一使用 objA == objB(省去了 ReferenceEquals 方法)。另外,为了范例简单,我们不考虑对象为null的情况。

我们来看第一段代码:

// 复制对象引用
bool result;
RefPoint rPoint1 = new RefPoint(1);
RefPoint rPoint2 = rPoint1;

result = (rPoint1 == rPoint2);      // 返回 true;
Console.WriteLine(result);

result = rPoint1.Equals(rPoint2);   // #2 返回true;
Console.WriteLine(result);

在阅读本文中,应该时刻在脑子里构思一个堆栈,一个堆,并思考着每条语句会在这两种结构上产生怎么样的效果。在这段代码中,产生的效果是:在堆上创建了一个新的RefPoint类型的实例(对象),并将它的x字段初始化为1;在堆栈上创建变量rPoint1,rPoint1保存堆上这个对象的地址;将rPoint1 赋值给 rPoint2时,此时并没有在堆上创建一个新的对象,而是将之前创建的对象的地址复制到了rPoint2。此时,rPoint1和rPoint2指向了堆上同一个对象。

从 ReferenceEquals()这个方法名就可以看出,它判断两个引用变量是不是指向了同一个变量,如果是,那么就返回true。这种相等叫做 引用相等(rPoint1 == rPoint2 等效于 ReferenceEquals)。因为它们指向的是同一个对象,所以对rPoint1的操作将会影响rPoint2:

注意System.Object静态的Equals(Object objA, Object objB)方法,在 #3 处,如果两个变量引用相等,那么将直接返回true。所以,可以预见我们上面的代码rPoint1.Equals(rPoint2); 在 #3 就会返回true。但是我们没有调用静态Equals(),直接调用了实体方法,最后调用了#2 的 InternalEquals(),返回true。(InternalEquals()无资料可查,仅通过调试测得)。

我们再看引用类型的第二种情况:

//创建新引用类型的对象,其成员的值相等
RefPoint rPoint1 = new RefPoint(1);
RefPoint rPoint2 = new RefPoint(1);

result = (rPoint1 == rPoint2);
Console.WriteLine(result);      // 返回 false;

result = rPoint1.Equals(rPoint2);
Console.WriteLine(result);      // #2 返回false

上面的代码在堆上创建了两个类型实例,并用同样的值初始化它们;然后将它们的地址分别赋值给堆上的变量 rPoint1和rPoint2。此时 #2 返回了false,可以看到,对于引用类型,即使类型的实例(对象)包含的值相等,如果变量指向的是不同的对象,那么也不相等。

2.简单值类型判等

注意本节的标题:简单值类型判等,这个简单是如何定义的呢?如果值类型的成员仅包含值类型,那么我们暂且管它叫 简单值类型,如果值类型的成员包含引用类型,我们管它叫复杂值类型。(注意,这只是本文中为了说明我个人作的定义。)

应该还记得我们之前提过,值类型都会隐式地继承自 System.ValueType类型,而ValueType类型覆盖了基类System.Object类型的Equals()方法,在值类型上调用Equals()方法,会调用ValueType的Equals()。所以,我们看看这个方法是什么样的,依然用 #number 标识后面会引用的地方。

public override bool Equals (Object obj) {
   if (null==obj) {
       return false;
   }
   RuntimeType thisType = (RuntimeType)this.GetType();
   RuntimeType thatType = (RuntimeType)obj.GetType();

   if (thatType!=thisType) { // 如果两个对象不是一个类型,直接返回false
       return false;  
   }

   Object thisObj = (Object)this;
   Object thisResult, thatResult;
 
   if (CanCompareBits(this))                // #5
       return FastEqualsCheck(thisObj, obj);    // #6

    // 利用反射获取值类型所有字段
   FieldInfo[] thisFields = thisType.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
    // 遍历字段,进行字段对字段比较
   for (int i=0; i<thisFields.Length; i++) {
       thisResult = ((RtFieldInfo)thisFields[i]).InternalGetValue(thisObj,false);
       thatResult = ((RtFieldInfo)thisFields[i]).InternalGetValue(obj, false);

       if (thisResult == null) {
           if (thatResult != null)
               return false;
       }
       else
       if (!thisResult.Equals(thatResult)) {  // #7
           return false;
       }
   }

   return true;
}

我们先来看看第一段代码:

// 复制结构变量
ValPoint vPoint1 = new ValPoint(1);
ValPoint vPoint2 = vPoint1;

result = (vPoint1 == vPoint2);  //编译错误:不能在ValPoint上应用 "==" 操作符
Console.WriteLine(result);  

result = Object.ReferenceEquals(vPoint1, vPoint2); // 隐式装箱,指向了堆上的不同对象
Console.WriteLine(result);          // 返回false

我们先在堆栈上创建了一个变量vPoint1,变量本身已经包含了所有字段和数据。然后在堆栈上复制了vPoint1的一份拷贝给了vPoint2,从常理思维上来讲,我们认为它应该是相等的。接下来我们就试着去比较它们,可以看到,我们不能用“==”直接去判断,这样会返回一个编译错误。如果我们调用System.Object基类的静态方法ReferenceEquals(),有意思的事情发生了:它返回了false。为什么呢?我们看下ReferenceEquals()方法的签名就可以了,它接受的是Object类型,也就是引用类型,而当我们传递vPoint1和vPoint2这两个值类型的时候,会进行一个隐式的装箱,效果相当于下面的语句:

Object boxPoint1 = vPoint1;
Object boxPoint2 = vPoint2;
result = (boxPoint1 == boxPoint2);      // 返回false
Console.WriteLine(result);             

而装箱的过程,我们在前面已经讲述过,上面的操作等于是在堆上创建了两个对象,对象包含的内容相同(地址不同),然后将对象地址分别返回给堆栈上的 boxPoint1和boxPoint2,再去比较boxPoint1和boxPoint2是否指向同一个对象,显然不是,所以返回false。

我们继续,添加下面这段代码:

result = vPoint1.Equals(vPoint2);       // #5 返回true; #6 返回true;
Console.WriteLine(result);      // 输出true

因为它们均继承自ValueType类型,所以此时会调用ValueType上的Equals()方法,在方法体内部,#5 CanCompareBits(this) 返回了true,CanCompareBits(this)这个方法,按微软的注释,意识是说:如果对象的成员中存在对于堆上的引用,那么返回false,如果不存在,返回true。按照ValPoint的定义,它仅包含一个int类型的字段x,自然不存在对堆上其他对象的引用,所以返回了true。从#5 的名字CanCompareBits,可以看出是判断是否可以进行按位比较,那么返回了true以后,#6 自然是进行按位比较了。

接下来,我们对vPoint2做点改动,看看会发生什么:

vPoint2.x = 2;
result = vPoint1.Equals(vPoint2);       // #5 返回true; #6 返回false;
Console.WriteLine(result);

3. 复杂值类型判等

到现在,上面的这些方法,我们还没有走到的位置,就是CanCompareBits返回false以后的部分了。前面我们已经推测出了CanCompareBits返回false的条件(值类型的成员包含引用类型),现在只要实现下就可以了。我们定义一个新的结构Line,它代表直线上的线段,我们让它的一个成员为值类型ValPoint,一个成员为引用类型RefPoint,然后去作比较。

/* 结构类型 ValLine 的定义,
public struct ValLine {
   public RefPoint rPoint;       // 引用类型成员
   public ValPoint vPoint;       // 值类型成员
   public Line(RefPoint rPoint, ValPoint vPoint) {
      this.rPoint = rPoint;
      this.vPoint = vPoint;
   }
}
*/

RefPoint rPoint = new RefPoint(1);
ValPoint vPoint = new ValPoint(1);

ValLine line1 = new ValLine (rPoint, vPoint);
ValLine line2 = line1;

result = line1.Equals(line2);   // 此时已经存在一个装箱操作,调用ValueType.Equals()
Console.WriteLine(result);      // 返回True

这个例子的过程要复杂得多。在开始前,我们先思考一下,当我们写下 line1.Equals(line2)时,已经进行了一个装箱的操作。如果要进一步判等,显然不能去判断变量是否引用的堆上同一个对象,这样的话就没有意义了,因为总是会返回false(装箱后堆上创建了两个对象)。那么应该如何判断呢?对 堆上对象 的成员(字段)进行一对一的比较,而成员又分为两种类型,一种是值类型,一种是引用类型。对于引用类型,去判断是否引用相等;对于值类型,如果是简单值类型,那么如同前一节讲述的去判断;如果是复杂类型,那么当然是递归调用了;最终直到要么是引用类型要么是简单值类型。

NOTE:进行字段对字段的一对一比较,需要用到反射,如果不了解反射,可以参看 .Net 中的反射 系列文章。

好了,我们现在看看实际的过程,是不是如同我们料想的那样,为了避免频繁的拖动滚动条查看ValueType的Equals()方法,我拷贝了部分下来:

public override bool Equals (Object obj) {
 
   if (CanCompareBits(this))                // #5
       return FastEqualsCheck(thisObj, obj);    // #6
    // 利用反射获取类型的所有字段(或者叫类型成员)
   FieldInfo[] thisFields = thisType.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
    // 遍历字段进行比较
   for (int i=0; i<thisFields.Length; i++) {
       thisResult = ((RtFieldInfo)thisFields[i]).InternalGetValue(thisObj,false);
       thatResult = ((RtFieldInfo)thisFields[i]).InternalGetValue(obj, false);

       if (thisResult == null) {
           if (thatResult != null)
               return false;
       }
       else
       if (!thisResult.Equals(thatResult)) {  #7
           return false;
       }
   }

   return true;
}

  1. 进入 ValueType 上的 Equals() 方法,#5 处返回了 false;
  2. 进入 for 循环,遍历字段。
  3. 第一个字段是RefPoint引用类型,#7 处,调用 System.Object 的Equals()方法,到达#2,返回true。
  4. 第二个字段是ValPoint值类型,#7 处,调用 System.ValType的Equals()方法,也就是当前方法本身。此处递归调用。
  5. 再次进入 ValueType 的 Equals() 方法,因为 ValPoint 为简单值类型,所以 #5 CanCompareBits 返回了true,接着 #6 FastEqualsCheck 返回了 true。
  6. 里层 Equals()方法返回 true。
  7. 退出 for 循环。
  8. 外层 Equals() 方法返回 true。

对象复制

有的时候,创建一个对象可能会非常耗时,比如对象需要从远程数据库中获取数据来填充,又或者创建对象需要读取硬盘文件。此时,如果已经有了一个对象,再创建新对象时,可能会采用复制现有对象的方法,而不是重新建一个新的对象。本节就讨论如何进行对象的复制。

1.浅度复制

浅度复制 和 深度复制 是以如何复制对象的成员(member)来划分的。一个对象的成员有可能是值类型,有可能是引用类型。当我们对对象进行一个浅度复制的时候,对于值类型成员,会复制其本身(值类型变量本身包含了所有数据,复制时进行按位拷贝);对于引用类型成员(注意它会引用另一个对象),仅仅复制引用,而不创建其引用的对象。结果就是:新对象的引用成员和 复制对象的引用成员 指向了同一个对象。

继续我们上面的例子,如果我们想要进行复制的对象(RefLine)是这样定义的,(为了避免look up,我在这里把代码再贴过来):

// 将要进行 浅度复制 的对象,注意为 引用类型
public class RefLine {
    public RefPoint rPoint;
    public ValPoint vPoint;
    public Line(RefPoint rPoint,ValPoint vPoint){
       this.rPoint = rPoint;
       this.vPoint = vPoint;
    }
}
// 定义一个引用类型成员
public class RefPoint {
    public int x;
    public RefPoint(int x) {
       this.x = x;
    }
}
// 定义一个值类型成员
public struct ValPoint {
    public int x;
    public ValPoint(int x) {
       this.x = x;
    }
}

我们先创建一个想要复制的对象:

RefPoint rPoint = new RefPoint(1);
ValPoint vPoint = new ValPoint(1);
RefLine line = new RefLine(rPoint, vPoint);

它所产生的实际效果是(堆栈上仅考虑line部分):

那么当我们对它复制时,就会像这样(newLine是指向新拷贝的对象的指针,在代码中体现为一个引用类型的变量):

按照这个定义,再回忆上面我们讲到的内容,可以推出这样一个结论:当复制一个结构类型成员的时候,直接创建一个新的结构类型变量,然后对它赋值,就相当于进行了一个浅度复制,也可以认为结构类型隐式地实现了浅度复制。如果我们将上面的RefLine定义为一个结构(Struct),结构类型叫ValLine,而不是一个类,那么对它进行浅度复制就可以这样:

ValLine newLine = line;

实际的效果图是这样:

现在你已经已经搞清楚了什么是浅度复制,知道了如何对结构浅度复制。那么如何对一个引用类型实现浅度复制呢?在.Net Framework中,有一个ICloneable接口,我们可以实现这个接口来进行浅度复制(也可以是深度复制,这里有争议,国外一些人认为ICloneable应该被标识为过时(Obsolete)的,并且提供IShallowCloneable和IDeepCloneble来替代)。这个接口只要求实现一个方法Clone(),它返回当前对象的副本。我们并不需要自己实现这个方法(当然完全可以),在System.Object基类中,有一个保护的MemeberwiseClone()方法,它便用于进行浅度复制。所以,对于引用类型,如果想要实现浅度复制时,只需要调用这个方法就可以了:

public object Clone() {
    return MemberwiseClone();
}

现在我们来做一个测试:

class Program {
    static void Main(string[] args) {

       RefPoint rPoint = new RefPoint(1);
       ValPoint vPoint = new ValPoint(1);
       RefLine line = new RefLine(rPoint, vPoint);

       RefLine newLine = (RefLine)line.Clone();
       Console.WriteLine("Original: line.rPoint.x = {0}, line.vPoint.x = {1}", line.rPoint.x, line.vPoint.x);
       Console.WriteLine("Cloned: newLine.rPoint.x = {0}, newLine.vPoint.x = {1}", newLine.rPoint.x, newLine.vPoint.x);

       line.rPoint.x = 10;      // 修改原先的line的 引用类型成员 rPoint
       line.vPoint.x = 10;      // 修改原先的line的 值类型  成员 vPoint
       Console.WriteLine("Original: line.rPoint.x = {0}, line.vPoint.x = {1}", line.rPoint.x, line.vPoint.x);
       Console.WriteLine("Cloned: newLine.rPoint.x = {0}, newLine.vPoint.x = {1}", newLine.rPoint.x, newLine.vPoint.x);

    }
}

输出为:

Original: line.rPoint.x = 1, line.vPoint.x = 1
Cloned: newLine.rPoint.x = 1, newLine.vPoint.x = 1
Original: line.rPoint.x = 10, line.vPoint.x = 10
Cloned: newLine.rPoint.x = 10, newLine.vPoint.x = 1

可见,复制后的对象和原先对象成了连体婴,它们的引用成员字段依然引用堆上的同一个对象。

2.深度复制

其实到现在你可能已经想到什么时深度复制了,深度复制就是将引用成员指向的对象也进行复制。实际的过程是创建新的引用成员指向的对象,然后复制对象包含的数据。

深度复制可能会变得非常复杂,因为引用成员指向的对象可能包含另一个引用类型成员,最简单的例子就是一个线性链表。

如果一个对象的成员包含了对于线性链表结构的一个引用,浅度复制 只复制了对头结点的引用,深度复制 则会复制链表本身,并复制每个结点上的数据。

考虑我们之前的例子,如果我们期望进行一个深度复制,我们的Clone()方法应该如何实现呢?

public object Clone(){       // 深度复制
    RefPoint rPoint = new RefPoint();       // 对于引用类型,创建新对象
    rPoint.x = this.rPoint.x;           // 复制当前引用类型成员的值 到 新对象
    ValPoint vPoint = this.vPoint;          // 值类型,直接赋值
    RefLine newLine = new RefLine(rPoint, vPoint);
    return newLine;
}

可以看到,如果每个对象都要这样去进行深度复制的话就太麻烦了,我们可以利用串行化/反串行化来对对象进行深度复制:先把对象串行化(Serialize)到内存中,然后再进行反串行化,通过这种方式来进行对象的深度复制:

public object Clone() {
    BinaryFormatter bf = new BinaryFormatter();
    MemoryStream ms = new MemoryStream();
    bf.Serialize(ms, this);
    ms.Position = 0;

    return (bf.Deserialize(ms)); ;
}

我们来做一个测试:

class Program {
    static void Main(string[] args) {
       RefPoint rPoint = new RefPoint(1);
       ValPoint vPoint = new ValPoint(2);

       RefLine line = new RefLine(rPoint, vPoint);
       RefLine newLine = (RefLine)line.Clone();
                 
       Console.WriteLine("Original line.rPoint.x = {0}", line.rPoint.x);
       Console.WriteLine("Cloned newLine.rPoint.x = {0}", newLine.rPoint.x);

       line.rPoint.x = 10;   // 改变原对象 引用成员 的值
       Console.WriteLine("Original line.rPoint.x = {0}", line.rPoint.x);
       Console.WriteLine("Cloned newLine.rPoint.x = {0}", newLine.rPoint.x);
    }
}
输出为:
Original line.rPoint.x = 1
Cloned newLine.rPoint.x = 1
Original line.rPoint.x = 10
Cloned newLine.rPoint.x = 1

可见,两个对象的引用成员已经分离,改变原对象的引用对象的值,并不影响复制后的对象。

这里需要注意:如果想将对象进行序列化,那么对象本身,及其所有的自定义成员(类、结构),都必须使用Serializable特性进行标记。所以,如果想让上面的代码运行,我们之前定义的类都需要进行这样的标记:

[Serializable()]
public class RefPoint { /*略*/}

NOTE:关于特性(Attribute),可以参考 .Net 中的反射(反射特性) 一文。

总结

本文简单地对C#中的类型作了一个回顾。

我们首先讨论了C#中的两种类型--值类型和引用类型,随后简要回顾了装箱/拆箱 操作。接着,详细讨论了C#中的对象判等。最后,我们讨论了浅度复制和 深度复制,并比较了它们之间不同。

希望这篇文章能给你带来帮助!

转载于:https://www.cnblogs.com/JimmyZhang/archive/2008/01/31/1059383.html

Guess you like

Origin blog.csdn.net/weixin_33892359/article/details/93444023