Link to home
Start Free TrialLog in
Avatar of San24
San24

asked on

Freeing Memory, Dispose, Class Objects

Experts,

What is the best way to clear an object of a class? Do I have to clear it from the Memory too? Should I be implementing Dispose()? If so - how?

Lets say I have a simple class. ClassSimple.cs. It contains a few properties and methods. I create an object of this class - CSimpleObj. I use it in the code and at some point I no longer need it. Lets say at a different point I need to use this but I don`t want any of the values from the previous instance. Do I do just a CSimpleObj = null or should I be actually clearing the object from the memory?

How would this effect the performance? If don`t do a cleanup.

Example.
//SimpleClass.cs
  class SimpleClass
    {
        private int testInt;
        public int TestInt
        {
            get
            {
                return testInt;
            }
            set
            {
                testInt = value;
            }
        }
    }

//Form.cs
  SimpleClass CSimpleObj = new SimpleClass();

        private void button1_Click(object sender, EventArgs e)
        {
            CSimpleObj.TestInt = 45;
            //Do something with CSimpleObj
            //Lets say, I no longer need the object.
            //I want to clear the object
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //I want an empty object here
            //Do something else here
        }

ASKER CERTIFIED SOLUTION
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of San24
San24

ASKER

Thanks everyone! Very helpful.