re-use an instance

M

Martijn Mulder

Can I call the constructor on an object that is already in memory? The
desired behavior is that I reset the state of an object with (one of the)
constructs of its type, but not have it allocate dynamic memory and not be
subjected to the garbage collector (until the process ends).
 
J

Jon Skeet [C# MVP]

Martijn said:
Can I call the constructor on an object that is already in memory?
No.

The desired behavior is that I reset the state of an object with (one of the)
constructs of its type, but not have it allocate dynamic memory and not be
subjected to the garbage collector (until the process ends).

In that case, you should implement a "Reset" method which resets the
internal state.

Jon
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Martijn Mulder said:
Can I call the constructor on an object that is already in memory?
No

The desired behavior is that I reset the state of an object with (one of
the) constructs of its type, but not have it allocate dynamic memory and
not be subjected to the garbage collector (until the process ends).

The way to go is to delegate the actual assignment of values to another
method, this method can be public and both the contructor and the resetter
can call it
 
S

Stoitcho Goutsev \(100\)

No, that's why it is called *constructor* because it constructs new objects.

Just move your code in a separate public method and use it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top