共用方式為


CultureInfo.Clone Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Creates a copy of the current CultureInfo object.

Namespace:  System.Globalization
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
Public Overridable Function Clone As Object
public virtual Object Clone()

Return Value

Type: System.Object
A copy of the current CultureInfo object.

Remarks

The Clone method creates a copy of the original object whose IsReadOnly method always returns false. This means that the clone is writable even if the original CultureInfo object is read-only. Therefore, the properties of the clone can be modified.

The Clone method creates an enhanced shallow copy. This means that, in addition to creating a copy of the original CultureInfo object, the method also creates copies of the objects returned by the NumberFormat, DateTimeFormat, TextInfo, and Calendar properties. Consequently, the cloned CultureInfo object can modify its copied properties without affecting the original CultureInfo object. (Most Clone methods return a shallow copy only. A shallow copy copies the current object but does not create copies of any objects returned by the current object's members. Instead, it refers to the original objects.)

Examples

The following example shows that Clone also clones the DateTimeFormatInfo and NumberFormatInfo instances associated with the CultureInfo.

Imports System.Globalization

Public Class Example
   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      ' Creates and initializes a CultureInfo object.
      Dim myCI As New CultureInfo("en-US")

      ' Clones myCI and modifies the DTFI and NFI instances associated with the clone.
      Dim myCIclone As CultureInfo = CType(myCI.Clone(), CultureInfo)
      myCIclone.DateTimeFormat.AMDesignator = "a.m."
      myCIclone.NumberFormat.CurrencySymbol = "USD"
      myCIclone.NumberFormat.NumberDecimalDigits = 4

      ' Displays the properties of the DTFI and NFI instances associated with the original and with the clone. 
      outputBlock.Text &= "DTFI/NFI PROPERTY" + ControlChars.Tab + "ORIGINAL" + ControlChars.Tab + "MODIFIED CLONE" & vbCrLf
      outputBlock.Text += String.Format("DTFI.AMDesignator" + ControlChars.Tab + "{0}" + ControlChars.Tab + ControlChars.Tab + "{1}", myCI.DateTimeFormat.AMDesignator, myCIclone.DateTimeFormat.AMDesignator) & vbCrLf
      outputBlock.Text += String.Format("NFI.CurrencySymbol" + ControlChars.Tab + "{0}" + ControlChars.Tab + ControlChars.Tab + "{1}", myCI.NumberFormat.CurrencySymbol, myCIclone.NumberFormat.CurrencySymbol) & vbCrLf
      outputBlock.Text += String.Format("NFI.NumberDecimalDigits" + ControlChars.Tab + "{0}" + ControlChars.Tab + ControlChars.Tab + "{1}", myCI.NumberFormat.NumberDecimalDigits, myCIclone.NumberFormat.NumberDecimalDigits) & vbCrLf
   End Sub  

End Class 
' This example produces the following output.
'       DTFI/NFI PROPERTY       ORIGINAL        MODIFIED CLONE
'       DTFI.AMDesignator       AM              a.m.
'       NFI.CurrencySymbol      $               USD
'       NFI.NumberDecimalDigits 2               4
using System;
using System.Globalization;

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      // Creates and initializes a CultureInfo object.
      CultureInfo myCI = new CultureInfo("en-US");

      // Clones myCI and modifies the DTFI and NFI instances associated with the clone.
      CultureInfo myCIclone = (CultureInfo)myCI.Clone();
      myCIclone.DateTimeFormat.AMDesignator = "a.m.";
      myCIclone.NumberFormat.CurrencySymbol = "USD";
      myCIclone.NumberFormat.NumberDecimalDigits = 4;

      // Displays the properties of the DTFI and NFI instances associated with the original and with the clone. 
      outputBlock.Text += "DTFI/NFI PROPERTY\tORIGINAL\tMODIFIED CLONE" + "\n";
      outputBlock.Text += String.Format("DTFI.AMDesignator\t{0}\t\t{1}", myCI.DateTimeFormat.AMDesignator, myCIclone.DateTimeFormat.AMDesignator) + "\n";
      outputBlock.Text += String.Format("NFI.CurrencySymbol\t{0}\t\t{1}", myCI.NumberFormat.CurrencySymbol, myCIclone.NumberFormat.CurrencySymbol) + "\n";
      outputBlock.Text += String.Format("NFI.NumberDecimalDigits\t{0}\t\t{1}", myCI.NumberFormat.NumberDecimalDigits, myCIclone.NumberFormat.NumberDecimalDigits) + "\n";
   }
}
/*
This code produces the following output.
   DTFI/NFI PROPERTY       ORIGINAL        MODIFIED CLONE
   DTFI.AMDesignator       AM              a.m.
   DTFI.DateSeparator      /               -
   NFI.CurrencySymbol      $               USD
   NFI.NumberDecimalDigits 2               4
*/

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.