Override new()

  • Thread starter Thread starter Henk
  • Start date Start date
H

Henk

Is it possible to override the new() function?

like in
TestObject to = new TestObject();

thnx,
Henk
 
Hello Henk,
Is it possible to override the new() function?

like in TestObject to = new TestObject();

thnx,
Henk

You can add a parametersless public constructor to the class, but I'm not
sure this is what you want. You cannot, as in C++, override the new function.

public class TestObject
{
public TestObject()
{
// your stuff here
}
}
 
Your question is not so clear, in fact there is not need for these, Why, its
all about patterns, means the new just create an instance of your class, that
all, but the only things you can do is just change the creation mode, for
example, do i want common object or singletone object or static objec, maybe
i need a factory.

So, new just define the pattern which the object create.
 
Henk said:
Is it possible to override the new() function?

like in TestObject to = new TestObject();

thnx,
Henk

I guess not. In C++ one overrides new operator for example to allocate
objects from somewhere else than heap (to build GC or something). Since
..NET allocates objects, you should let it do its work. However, you
might succeed in refractoring the constructor calls using Reflection.Emit.
 

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

Back
Top