Variable declaration/definition

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

    Variable declaration/definition

    I am relatively new in C#, but have several years C++/Java. I am writing
    a little C# console app to "test the waters" and learn the language.

    If I have a class defined as:

    class MyClass
    {
    MyClass(string s1, int i2, bool b3)
    {
    }
    }


    Why can't I declare a variable on the stack somewhere later on in my
    code like this:

    // .... code above
    MyClass mc("Hello",42, true);

    Do I always have to alloc from the heap when instantiating objects whose
    ctor have parameters?

  • Paul E Collins

    #2
    Re: Variable declaration/definition

    "Annonymous Coward" <me@home.comwro te:
    Why can't I declare a variable on the stack somewhere later on in my code
    like this:
    // .... code above
    MyClass mc("Hello",42, true);
    AIUI, variables of reference types (which includes all class instances) are
    always created on the heap.

    Eq.


    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: Variable declaration/definition

      Annonymous Coward <me@home.comwro te:
      I am relatively new in C#, but have several years C++/Java. I am writing
      a little C# console app to "test the waters" and learn the language.
      >
      If I have a class defined as:
      >
      class MyClass
      {
      MyClass(string s1, int i2, bool b3)
      {
      }
      }
      >
      >
      Why can't I declare a variable on the stack somewhere later on in my
      code like this:
      >
      // .... code above
      MyClass mc("Hello",42, true);
      That's not how you invoke a constructor in C#. You can just declare a
      variable, of course:

      MyClass mc;

      but that won't create an instance.
      Do I always have to alloc from the heap when instantiating objects whose
      ctor have parameters?
      You always have to allocate from the heap whenever you create *any*
      objects, regardless of whether there are parameterless constructors. In
      that respect C# is identical to Java. Basically C# is closer to Java
      than it is to C++ in many ways.

      Note, however, that "new" doesn't always mean "allocate from the heap".
      For instance, consider:

      DateTime dt = new DateTime(2008, 06, 26);

      That calls the DateTime constructor, but doesn't allocate anything on
      the heap (assuming it's a normal local variable), because DateTime is a
      value type. In this respect C# is different to Java, as Java doesn't
      have custom value types, just the primitives.

      --
      Jon Skeet - <skeet@pobox.co m>
      Web site: http://www.pobox.com/~skeet
      Blog: http://www.msmvps.com/jon_skeet
      C# in Depth: http://csharpindepth.com

      Comment

      • Peter Duniho

        #4
        Re: Variable declaration/definition

        On Thu, 26 Jun 2008 11:43:04 -0700, Paul E Collins
        <find_my_real_a ddress@CL4.orgw rote:
        "Annonymous Coward" <me@home.comwro te:
        >
        >Why can't I declare a variable on the stack somewhere later on in my
        >code
        >like this:
        >// .... code above
        >MyClass mc("Hello",42, true);
        >
        AIUI, variables of reference types (which includes all class instances)
        are
        always created on the heap.
        Just to clarify: what Paul means is that the _instances_ are created on
        the heap. The variable storing the reference to that instance itself may
        well be on the stack (in the case of local variables or method arguments).

        Pete

        Comment

        • Jon Skeet [C# MVP]

          #5
          Re: Variable declaration/definition

          Paul E Collins <find_my_real_a ddress@CL4.orgw rote:
          "Annonymous Coward" <me@home.comwro te:
          >
          Why can't I declare a variable on the stack somewhere later on in my code
          like this:
          // .... code above
          MyClass mc("Hello",42, true);
          >
          AIUI, variables of reference types (which includes all class instances) are
          always created on the heap.
          Nearly. The variables themselves are created wherever their context
          says they should be - but the *objects* are always on the heap.

          --
          Jon Skeet - <skeet@pobox.co m>
          Web site: http://www.pobox.com/~skeet
          Blog: http://www.msmvps.com/jon_skeet
          C# in Depth: http://csharpindepth.com

          Comment

          • Jon Skeet [C# MVP]

            #6
            Re: Variable declaration/definition

            Jon Skeet [C# MVP] <skeet@pobox.co mwrote:
            Do I always have to alloc from the heap when instantiating objects whose
            ctor have parameters?
            >
            You always have to allocate from the heap whenever you create *any*
            objects, regardless of whether there are parameterless constructors. In
            that respect C# is identical to Java. Basically C# is closer to Java
            than it is to C++ in many ways.
            What I realised I hadn't made clear after I'd posted is that the
            variable itself will still live on the stack if it's a local variable
            (which isn't being captured or part of an iterator block). The value of
            the variable is a reference, and that reference points to somewhere on
            the heap. (This is assuming we're still talking about a reference type
            variable, of course.)

            --
            Jon Skeet - <skeet@pobox.co m>
            Web site: http://www.pobox.com/~skeet
            Blog: http://www.msmvps.com/jon_skeet
            C# in Depth: http://csharpindepth.com

            Comment

            • Annonymous Coward

              #7
              Re: Variable declaration/definition

              Jon Skeet [C# MVP] wrote:
              Annonymous Coward <me@home.comwro te:
              >I am relatively new in C#, but have several years C++/Java. I am writing
              >a little C# console app to "test the waters" and learn the language.
              >>
              >If I have a class defined as:
              >>
              >class MyClass
              >{
              > MyClass(string s1, int i2, bool b3)
              > {
              > }
              >}
              >>
              >>
              >Why can't I declare a variable on the stack somewhere later on in my
              >code like this:
              >>
              >// .... code above
              >MyClass mc("Hello",42, true);
              >
              That's not how you invoke a constructor in C#. You can just declare a
              variable, of course:
              >
              MyClass mc;
              >
              but that won't create an instance.
              >
              >Do I always have to alloc from the heap when instantiating objects whose
              >ctor have parameters?
              >
              You always have to allocate from the heap whenever you create *any*
              objects, regardless of whether there are parameterless constructors. In
              that respect C# is identical to Java. Basically C# is closer to Java
              than it is to C++ in many ways.
              >
              Note, however, that "new" doesn't always mean "allocate from the heap".
              For instance, consider:
              >
              DateTime dt = new DateTime(2008, 06, 26);
              >
              That calls the DateTime constructor, but doesn't allocate anything on
              the heap (assuming it's a normal local variable), because DateTime is a
              value type. In this respect C# is different to Java, as Java doesn't
              have custom value types, just the primitives.
              >
              Thanks Jon - its very clear to me now. tx

              Comment

              Working...