DicomObjects.NET.8.48 Documentation
DicomObjects Namespace / DicomDataSet Class / Clone Method / Clone(Boolean) Method

If true then returns a Deep copy of the DicomDataSet.

If false, returns a Shallow copy.

Example

In This Topic
    Clone(Boolean) Method
    In This Topic
    Returns a copy of the DicomDataSet
    Syntax
    'Declaration
     
    
    Public Overloads Overridable Function Clone( _
       ByVal Deep As System.Boolean _
    ) As System.Object
    public virtual System.object Clone( 
       System.bool Deep
    )

    Parameters

    Deep

    If true then returns a Deep copy of the DicomDataSet.

    If false, returns a Shallow copy.

    Return Value

    Remarks

    This method makes either a Shallow Copy or a Deep Copy of the DicomDataSet, dependants on the Deep parameter.

    A shallow copy will only have the top level objects replicated, any referenced objects under the top level will be shared by the original DicomDataSet and the cloned copy.

    Example
    // Load the original dataset from a file
    DicomDataSet ds = new DicomDataSet("Dataset.dcm");
                
    // Create a nested DicomDataSet to include inside a sequence
    DicomDataSet sharedItem = new DicomDataSet();
    sharedItem.Add(Keyword.RequestingPhysician, "Dr. Smith");
    sharedItem.Add(Keyword.RequestedProcedureID, "PROC123");
    sharedItem.Add(Keyword.PlacerOrderNumberProcedure, "ORDER456");
    sharedItem.Add(Keyword.FillerOrderNumberProcedure, "FILL789");
    sharedItem.Add(Keyword.AccessionNumber, "ACC000123");
    sharedItem.Add(Keyword.ScheduledProcedureStepDescription, "CT Abdomen with Contrast");
                
    // Create a sequence and add the nested item to it
    DicomDataSetCollection sequenceCollection = new DicomDataSetCollection();
    sequenceCollection.Add(sharedItem);
                
    // Add the sequence to the main dataset under RequestAttributesSequence
    ds.Add(Keyword.RequestAttributesSequence, sequenceCollection);
                
    // Create a shallow clone (shares nested structures like sequences)
    var shallowClone = ds.Clone(false) as DicomDataSet;
                         
    // Create a deep clone (independent copy of top-level and nested data)
    var deepClone = ds.Clone(true) as DicomDataSet;
                
    // Modify a nested structure in the shallow clone (this will affect the original)
    var sequence = shallowClone[Keyword.RequestAttributesSequence].Value as DicomDataSetCollection;
    if (sequence != null && sequence.Count > 0)
    {
        sequence[0].Add(Keyword.RequestingPhysician, "Changed in Shallow Clone");
    }
                
    // Retrieve sequences to compare effects of the modification
    var origSeq = ds[Keyword.RequestAttributesSequence].Value as DicomDataSetCollection;
    var shallowSeq = shallowClone[Keyword.RequestAttributesSequence].Value as DicomDataSetCollection;       
    var deepSeq = deepClone[Keyword.RequestAttributesSequence].Value as DicomDataSetCollection;
                
    Console.WriteLine("Original RequestingPhysician: " + origSeq[0][Keyword.RequestingPhysician].Value);
    Console.WriteLine("ShallowClone RequestingPhysician: " + shallowSeq[0][Keyword.RequestingPhysician].Value);        
    Console.WriteLine("DeepClone RequestingPhysician: " + deepSeq[0][Keyword.RequestingPhysician].Value);
                
    // Output:
    // Original RequestingPhysician: Changed in Shallow Clone
    // ShallowClone RequestingPhysician: Changed in Shallow Clone
    // DeepClone RequestingPhysician: Dr. Smith
                
    // Explanation:
    // - Top-level tags like PatientName are independently copied in both clones.
    // - The shallow clone shares references to nested sequences with the original dataset.
    // - Therefore, modifying a nested item in the shallow clone also modifies the original.
    // - The deep clone is completely isolated, and its nested structures remain unchanged.
    Requirements

    Target Platforms: .NET CLR 4.8 or higher

    See Also