Override new()

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Henk

    #1

    Override new()

    Is it possible to override the new() function?

    like in
    TestObject to = new TestObject();

    thnx,
    Henk


  • Jon Skeet [C# MVP]

    #2
    Re: Override new()

    Henk <nospam@geenema il.nlwrote:
    Is it possible to override the new() function?
    >
    like in
    TestObject to = new TestObject();
    No.

    --
    Jon Skeet - <skeet@pobox.co m>
    http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
    If replying to the group, please do not mail me too

    Comment

    • Jesse Houwing

      #3
      Re: Override new()

      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
      }
      }

      --
      Jesse Houwing
      jesse.houwing at sogeti.nl


      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: Override new()

        Jon Skeet [C# MVP] <skeet@pobox.co mwrote:
        Henk <nospam@geenema il.nlwrote:
        Is it possible to override the new() function?

        like in
        TestObject to = new TestObject();
        >
        No.
        Sorry, that wasn't a terribly helpful answer.

        Could you explain more about what you're trying to accomplish?

        --
        Jon Skeet - <skeet@pobox.co m>
        http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
        If replying to the group, please do not mail me too

        Comment

        • =?Utf-8?B?WWFyb24gS2Fybmk=?=

          #5
          RE: Override new()

          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.
          --
          Sincerely
          Yaron Karni
          All .net c# stuff, patterns code and snippet help programmer.



          "Henk" wrote:
          Is it possible to override the new() function?
          >
          like in
          TestObject to = new TestObject();
          >
          thnx,
          Henk
          >
          >
          >

          Comment

          • Arto V Viitanen

            #6
            Re: Override new()

            Henk wrote:
            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 .

            --
            Arto Viitanen

            Comment

            Working...