I have an object like this.

List<TSQLBeginTransactionBlock_Ext> beginTranList =new List<TSQLBeginTransactionBlock_Ext>();

Now i want to make some changes to this object but i want the original object as well.

I tried created a temporary object like this

List<TSQLBeginTransactionBlock_Ext> tempBeginTranList =new List<TSQLBeginTransactionBlock_Ext>();
tempBeginTranList = beginTranList;

then I've made changes to this temp object. But since class objects are of reference types the changes
were automatically made to the beginTranList object.

Can somebody help me out with this?

Well I don't know the class TSQLBeginTransactionBlock_Ext so I guess that's your own class. To make a deep copy of the Object in C# you should use Clone method from IClonable. You can also add a constructor to TSQLBeginTransactionBlock_Ext which takes as a parameter a variable of the same type as the class in which it is defined.
Simple example:

struct Point
    {
        private int x;
        private int y;

        public int X { get { return x; } set { x = value; } }
        public int Y { get { return y; } set { y = value; } }

        public Point(int x, int y)
        {
            this.x = x;
            this.y = y;
        }
    }

    class Location
    {
        private Point position;
        private string locationName;

        public Location(string locationName, Point position)
        {
            this.locationName = locationName;
            this.position = position;
        }
        // makes deep copy
        public Location(Location prototype)
        {
            locationName = prototype.locationName;
            position = prototype.position;
        }

        public void ChangeLocation(int x, int y)
        {
            this.position.X = x;
            this.position.Y = y;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Point pkt = new Point(10, 20);
            Location loc = new Location("New York", pkt);
            Location deepCopy = new Location(loc);
            loc.ChangeLocation(30, 50);
            Console.ReadLine();
        }
    }

The previous poster did a good job at answering your question. But I am here to tell you why what you tried didn't work.

your code

List<TSQLBeginTransactionBlock_Ext> tempBeginTranList =new List<TSQLBeginTransactionBlock_Ext>();
tempBeginTranList = beginTranList;

Now its important to know that when you create a variable with a type. You are really creating an object reference, or object pointer.

when you say List<TSQLBeginTransactionBlock_Ext> tempBeginTranList what you are saying is, create a reference by the name TempBeginTranList, and have it always point an object of type List<TSQLBeginTransactionBlock_Ext>.

now when you say TempBeginTranList = new List<TSQLBeginTransactionBlock_Ext>(); you are saying the TempBeginTranList object reference should point to a new instance of List<TSQLBeginTransactionBlock_Ext>. which is created by calling that constructor method with the keyword new.

but when you say. tempBeginTranList = beginTranList; you are actually saying the Object reference tempBeginTranList should now point to the object beginTranList is pointing at, So now both of them are pointing to the original object in memory. and the List you created with new and assigned to TempBeginTranList original now has no references, and is both lost, and eligible for garbage collection.


Any time you want to create another object. you need to use that word new, now assuming all the properties of the objects are public you can use reflection to loop through the properties in a type and duplicate it. Since your object is a list it makes it MUCH easier.

List<TSQLBeginTransactionBlock_Ext> tempBeginTranList =new List<TSQLBeginTransactionBlock_Ext>();
tempBeginTranList.AddRange(beginTranList);

so now the 2 lists contain the same objects. Its important to note that the lists also contain references to the objects, not the objects themselves. so if you change the properties of an object in one list, those properties should reflect those changes in the other list.
That goes to say though. if you remove objects from one list, they still exist. because the other list holds a reference to it keeping it from being garbage collected.

I hope this helps you understand Objects and variables a bit better.

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.