CultureInfo.Clone Method

July 28, 2014

Creates a copy of the current CultureInfo object.

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

public virtual Object Clone()

Return Value

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

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.)

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


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
*/


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2014 Microsoft