NumberFormatInfo.Clone 方法


建立 NumberFormatInfo 物件的淺層複本 (Shallow Copy)。Creates a shallow copy of the NumberFormatInfo object.

 virtual System::Object ^ Clone();
public object Clone ();
abstract member Clone : unit -> obj
override this.Clone : unit -> obj
Public Function Clone () As Object


從原始 NumberFormatInfo 物件複製的新物件。A new object copied from the original NumberFormatInfo object.



下列範例會使用Clone方法來建立NumberFormatInfo物件的讀取/寫入複本,其代表目前線程文化特性的數值格式設定慣例。The following example uses the Clone method to create a read/write copy of a NumberFormatInfo object that represents the numeric formatting conventions of the current thread culture.

using System;
using System.Globalization;

public class Example
   public static void Main()
      NumberFormatInfo nfi = NumberFormatInfo.CurrentInfo;
      Console.WriteLine("Read-Only: {0}\n", nfi.IsReadOnly);

      NumberFormatInfo nfiw = (NumberFormatInfo) nfi.Clone();
      Console.WriteLine("Read-Only: {0}", nfiw.IsReadOnly);
// The example displays the following output:
//       Read-Only: True
//       Read-Only: False
Imports System.Globalization

Module Example
   Public Sub Main()
      Dim nfi As NumberFormatInfo = NumberFormatInfo.CurrentInfo
      Console.WriteLine("Read-Only: {0}", nfi.IsReadOnly)
      Dim nfiw As NumberFormatInfo = CType(nfi.Clone(), NumberFormatInfo)
      Console.WriteLine("Read-Only: {0}", nfiw.IsReadOnly)
   End Sub
End Module
' The example displays the following output:
'       Read-Only: True
'       Read-Only: False


即使原始NumberFormatInfo物件是唯讀的,複製也是可寫入的。The clone is writable even if the original NumberFormatInfo object is read-only. 因此,可以使用使用者定義模式來修改複本的屬性。Therefore, the properties of the clone can be modified with user-defined patterns.

物件的淺層複本只是物件的複本。A shallow copy of an object is a copy of the object only. 如果物件包含其他物件的參考,淺層複製就不會建立所參考物件的複本。If the object contains references to other objects, the shallow copy will not create copies of the referred objects. 它會改為參考原始物件。It will refer to the original objects instead. 另一方面,物件的深層複本會建立物件的複本,以及該物件直接或間接參考的所有專案複本。On the other hand, a deep copy of an object creates a copy of the object and a copy of everything directly or indirectly referenced by that object. NumberFormatInfo物件的情況下,淺層複製就足以複製所有實例屬性,因為傳回物件參考的所有屬性都是staticShared在 Visual Basic 中)。In the case of a NumberFormatInfo object, a shallow copy is sufficient for copying all instance properties, because all properties that return object references are static (Shared in Visual Basic).