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