C# syntax

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

    C# syntax

    Hello there.

    Why does one need to use "new" keyword in C# in order to construct object?
    Does semantic of constructor invoke have other meaning except of object
    construction in the heap (or value type in a stack)?
    --
    Vladimir Nesterovsky
    visit: http://constcli.sscli.net
    e-mail: nesterovsky@ssc li.net, vladimir@nester ovsky-bros.com
    home: http://www.nesterovsky-bros.com


  • Jon Skeet [C# MVP]

    #2
    Re: C# syntax

    Vladimir Nesterovsky <vladimir@neste rovsky-bros.com> wrote:[color=blue]
    > Why does one need to use "new" keyword in C# in order to construct object?[/color]

    It helps to make the difference between methods and constructors
    obvious.
    [color=blue]
    > Does semantic of constructor invoke have other meaning except of object
    > construction in the heap (or value type in a stack)?[/color]

    Not as far as I'm aware - what kind of thing were you thinking of?

    --
    Jon Skeet - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    • Vladimir Nesterovsky

      #3
      Re: C# syntax

      > > Why does one need to use "new" keyword in C# in order to construct
      object?[color=blue]
      >
      > It helps to make the difference between methods and constructors
      > obvious.
      >[color=green]
      > > Does semantic of constructor invoke have other meaning except of object
      > > construction in the heap (or value type in a stack)?[/color]
      >
      > Not as far as I'm aware - what kind of thing were you thinking of?[/color]

      I just noticed some code that is very overloaded with this keyword. :-) In
      C# it has no useful semantic meaning in my opinion.

      --
      Vladimir Nesterovsky
      visit: http://constcli.sscli.net
      e-mail: nesterovsky@ssc li.net, vladimir@nester ovsky-bros.com
      home: http://www.nesterovsky-bros.com


      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: C# syntax

        Vladimir Nesterovsky <vladimir@neste rovsky-bros.com> wrote:[color=blue][color=green]
        > > Not as far as I'm aware - what kind of thing were you thinking of?[/color]
        >
        > I just noticed some code that is very overloaded with this keyword. :-)[/color]

        What do you mean by "overloaded " here?
        [color=blue]
        > In C# it has no useful semantic meaning in my opinion.[/color]

        Its use is in terms of readability, and I for one am glad it's there.

        --
        Jon Skeet - <skeet@pobox.co m>
        Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

        If replying to the group, please do not mail me too

        Comment

        • Morten Wennevik

          #5
          Re: C# syntax

          Maybe

          MyObject mobj; // mobj is currently empty and needs to be assigned an
          object
          mobj = new MyObject();
          OR
          mobj = new MyObject(int number);
          OR
          mobj = new MyObject(int string);
          OR
          ....

          The new keyword simply indicates that a new MyObject is created and the
          reference to the object is put into mobj.

          --
          Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/

          Comment

          • Guest's Avatar

            #6
            Re: C# syntax

            overloading just means that, different ways of constructing, look at the
            relevant ctor to see what its doing (usually just setting fields)


            "Morten Wennevik" <MortenWennevik @hotmail.com> wrote in message
            news:oprykosahn hntkfz@localhos t...
            Maybe

            MyObject mobj; // mobj is currently empty and needs to be assigned an
            object
            mobj = new MyObject();
            OR
            mobj = new MyObject(int number);
            OR
            mobj = new MyObject(int string);
            OR
            ....

            The new keyword simply indicates that a new MyObject is created and the
            reference to the object is put into mobj.

            --
            Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/


            Comment

            • Daniel O'Connell

              #7
              Re: C# syntax


              "Vladimir Nesterovsky" <vladimir@neste rovsky-bros.com> wrote in message
              news:OyvGMycqDH A.372@TK2MSFTNG P11.phx.gbl...[color=blue]
              > Hello there.
              >
              > Why does one need to use "new" keyword in C# in order to construct object?
              > Does semantic of constructor invoke have other meaning except of object
              > construction in the heap (or value type in a stack)?[/color]
              It seems rather unlikely, but the following code construct can exist:

              class MyTestObject
              {

              public MyTestObject()
              {
              //do constructor stuff
              }
              }

              class MyOtherObject
              {
              //the meanings are different between these two.
              //this one obviously creates a new object;
              MyTestObject myObject = new MyTestObject();
              //but what does this one do?
              MyTestObject myObject = MyTestObject();

              MyTestObject MyTestObject()
              {
              return null;
              }

              }

              without using the new keyword, this code would be invalid, which would be
              VERY unpleasent when someone decided to come up with a class with the same
              name as one of your methods. As I said, its unlikely, but it IS possible.
              [color=blue]
              > --
              > Vladimir Nesterovsky
              > visit: http://constcli.sscli.net
              > e-mail: nesterovsky@ssc li.net, vladimir@nester ovsky-bros.com
              > home: http://www.nesterovsky-bros.com
              >
              >[/color]


              Comment

              • Jon Skeet [C# MVP]

                #8
                Re: C# syntax

                <<.>> wrote:[color=blue]
                > overloading just means that, different ways of constructing, look at the
                > relevant ctor to see what its doing (usually just setting fields)[/color]

                Yes, I know what overloading is, but it doesn't seem to fit in with
                "some code which is very overloaded with this keyword".

                --
                Jon Skeet - <skeet@pobox.co m>
                Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                If replying to the group, please do not mail me too

                Comment

                • Morten Wennevik

                  #9
                  Re: C# syntax

                  This is confusing because the method MyTestObject has the same name as the
                  class MyTestObject

                  You could do
                  string mystring;
                  mystring = MyTestObject();

                  but this MyTestObject has nothing to do with the class of the same name.
                  With the new keyword you create a new object of the type name
                  MyTestObject. Framework then assigns memory to fit the new object and
                  calls the constructor for the object.


                  --
                  Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/

                  Comment

                  • Morten Wennevik

                    #10
                    Re: C# syntax

                    Well, you would probably get a compiler error seeing as MyTestObject()
                    returns a MyTestObject

                    --
                    Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/

                    Comment

                    • Kirill Ritvinsky

                      #11
                      Re: C# syntax


                      Jon Skeet [C# MVP] wrote:
                      [color=blue]
                      > <<.>> wrote:
                      >[color=green]
                      >>overloading just means that, different ways of constructing, look at the
                      >>relevant ctor to see what its doing (usually just setting fields)[/color]
                      >
                      >
                      > Yes, I know what overloading is, but it doesn't seem to fit in with
                      > "some code which is very overloaded with this keyword".
                      >[/color]

                      I think, "some code which is very overloaded with this keyword" means
                      that the "new" keyword occurs too many times in some portion of code.

                      It's just not very applicable translation from Russian :)

                      --
                      Best wishes,
                      Kirill Ritvinsky
                      Get this domain name before someone else does. Quick and painless shopping. Affordable payment options available.


                      Comment

                      • Daniel O'Connell

                        #12
                        Re: C# syntax

                        Hrmm, your posts won't quote in OE, thats weird.

                        Anyway, thats exactly the point. He asked why the new keyword, so in the
                        above case when
                        MyTestObject obj = MyTestObject();
                        is called, what will obj be? Will it call the method or will it call the
                        constructor of MyTestObject. Without the new keyword, a strange spec
                        declaration would have to exist to differentiate between these situations.
                        The point of it was to illustrate the need for the new keyword in object
                        construction. The new keyword at the very least differentiates from this
                        problem.

                        "Morten Wennevik" <MortenWennevik @hotmail.com> wrote in message
                        news:opryksn7uo hntkfz@localhos t...
                        This is confusing because the method MyTestObject has the same name as the
                        class MyTestObject

                        You could do
                        string mystring;
                        mystring = MyTestObject();

                        but this MyTestObject has nothing to do with the class of the same name.
                        With the new keyword you create a new object of the type name
                        MyTestObject. Framework then assigns memory to fit the new object and
                        calls the constructor for the object.


                        --
                        Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/


                        Comment

                        Working...