CultureInfo.Clone Method
Creates a copy of the current CultureInfo.
[Visual Basic] Public Overridable Function Clone() As Object Implements _ ICloneable.Clone [C#] public virtual object Clone(); [C++] public: virtual Object* Clone(); [JScript] public function Clone() : Object;
Return Value
A copy of the current CultureInfo.
Implements
Remarks
The clone is writable even if the original CultureInfo is read-only; therefore, the properties of the clone can be modified.
A shallow copy of an object is a copy of the object only. If the object contains references to other objects, the shallow copy does not create copies of the referred objects. It refers to the original objects instead. In contrast, a deep copy of an object creates a copy of the object and a copy of everything directly or indirectly referenced by that object.
CultureInfo.Clone is a shallow copy with exceptions. The objects returned by the NumberFormat and the DateTimeFormat properties are also cloned, so that the CultureInfo clone can modify the properties of NumberFormat and DateTimeFormat without affecting the original CultureInfo.
Example
[Visual Basic, C#, C++] The following code example shows that CultureInfo.Clone also clones the DateTimeFormatInfo and NumberFormatInfo instances associated with the CultureInfo.
[Visual Basic] Imports System Imports System.Globalization Public Class SamplesCultureInfo Public Shared Sub Main() ' Creates and initializes a CultureInfo. Dim myCI As New CultureInfo("en-US", False) ' 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.DateTimeFormat.DateSeparator = "-" 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. Console.WriteLine("DTFI/NFI PROPERTY" + ControlChars.Tab + "ORIGINAL" + ControlChars.Tab + "MODIFIED CLONE") Console.WriteLine("DTFI.AMDesignator" + ControlChars.Tab + "{0}" + ControlChars.Tab + ControlChars.Tab + "{1}", myCI.DateTimeFormat.AMDesignator, myCIclone.DateTimeFormat.AMDesignator) Console.WriteLine("DTFI.DateSeparator" + ControlChars.Tab + "{0}" + ControlChars.Tab + ControlChars.Tab + "{1}", myCI.DateTimeFormat.DateSeparator, myCIclone.DateTimeFormat.DateSeparator) Console.WriteLine("NFI.CurrencySymbol" + ControlChars.Tab + "{0}" + ControlChars.Tab + ControlChars.Tab + "{1}", myCI.NumberFormat.CurrencySymbol, myCIclone.NumberFormat.CurrencySymbol) Console.WriteLine("NFI.NumberDecimalDigits" + ControlChars.Tab + "{0}" + ControlChars.Tab + ControlChars.Tab + "{1}", myCI.NumberFormat.NumberDecimalDigits, myCIclone.NumberFormat.NumberDecimalDigits) End Sub 'Main End Class 'SamplesCultureInfo ' 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 [C#] using System; using System.Globalization; public class SamplesCultureInfo { public static void Main() { // Creates and initializes a CultureInfo. CultureInfo myCI = new CultureInfo("en-US", false); // Clones myCI and modifies the DTFI and NFI instances associated with the clone. CultureInfo myCIclone = (CultureInfo) myCI.Clone(); myCIclone.DateTimeFormat.AMDesignator = "a.m."; myCIclone.DateTimeFormat.DateSeparator = "-"; 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. Console.WriteLine( "DTFI/NFI PROPERTY\tORIGINAL\tMODIFIED CLONE" ); Console.WriteLine( "DTFI.AMDesignator\t{0}\t\t{1}", myCI.DateTimeFormat.AMDesignator, myCIclone.DateTimeFormat.AMDesignator ); Console.WriteLine( "DTFI.DateSeparator\t{0}\t\t{1}", myCI.DateTimeFormat.DateSeparator, myCIclone.DateTimeFormat.DateSeparator ); Console.WriteLine( "NFI.CurrencySymbol\t{0}\t\t{1}", myCI.NumberFormat.CurrencySymbol, myCIclone.NumberFormat.CurrencySymbol ); Console.WriteLine( "NFI.NumberDecimalDigits\t{0}\t\t{1}", myCI.NumberFormat.NumberDecimalDigits, myCIclone.NumberFormat.NumberDecimalDigits ); } } /* 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 */ [C++] #using <mscorlib.dll> using namespace System; using namespace System::Globalization; int main() { // Creates and initializes a CultureInfo. CultureInfo* myCI = new CultureInfo(S"en-US", false); // Clones myCI and modifies the DTFI and NFI instances associated with the clone. CultureInfo * myCIclone = dynamic_cast<CultureInfo*>(myCI -> Clone()); myCIclone -> DateTimeFormat -> AMDesignator = S"a.m."; myCIclone -> DateTimeFormat -> DateSeparator = S"-"; myCIclone -> NumberFormat -> CurrencySymbol = S"USD"; myCIclone -> NumberFormat -> NumberDecimalDigits = 4; // Displays the properties of the DTFI and NFI instances associated with the original and with the clone. Console::WriteLine(S"DTFI/NFI PROPERTY\tORIGINAL\tMODIFIED CLONE"); Console::WriteLine(S"DTFI.AMDesignator\t{0}\t\t{1}", myCI -> DateTimeFormat -> AMDesignator, myCIclone -> DateTimeFormat -> AMDesignator); Console::WriteLine(S"DTFI.DateSeparator\t{0}\t\t{1}", myCI -> DateTimeFormat -> DateSeparator, myCIclone -> DateTimeFormat -> DateSeparator); Console::WriteLine(S"NFI.CurrencySymbol\t{0}\t\t{1}", myCI -> NumberFormat -> CurrencySymbol, myCIclone -> NumberFormat -> CurrencySymbol); Console::WriteLine(S"NFI.NumberDecimalDigits\t{0}\t\t{1}", __box(myCI -> NumberFormat -> NumberDecimalDigits), __box(myCIclone -> NumberFormat -> NumberDecimalDigits)); } /* 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 */
[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button
in the upper-left corner of the page.
Requirements
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework
See Also
CultureInfo Class | CultureInfo Members | System.Globalization Namespace | Object