hi athlets!))
please tell me - how it's possible to copy object in c#? (get it not by reference)
does anybody know a good article to understand this process?
on msdn I've found only this , but it's not clear..

thanks in advance!

I use this extension method to perform deep clones of objects:

using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;     
      
public static class ObjectCopier {
    public static T Clone<T>(this T source) {
        if (!typeof(T).IsSerializable) {
            throw new ArgumentException("The type must be serializable.", "source");
        }
  
        if (Object.ReferenceEquals(source, null)) {
            return default(T);
        }

        IFormatter formatter = new BinaryFormatter();
        using (Stream stream = new MemoryStream()) {
            formatter.Serialize(stream, source);
            stream.Seek(0, SeekOrigin.Begin);
            return (T)formatter.Deserialize(stream);
        }
    }
}

(Note: Not originally my code but it's been so long I don't know where it came from :)).

Are you are trying to figure out what is the difference between a deep and shallow copy?

commented: ++++++++++ +3
commented: Good post :) +14

I know the difference...) but it's too sirious code for me...) thank you Momerath)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.