design question : struct or class

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

    #16
    Re: design question : struct or class

    As Jon has stated, objects, by default, are passed by value. Structures
    have the overhead of value semantics, as I see it, just like C++
    classes,
    but without the benefit of C++'s deterministic destructors. Thus the
    limited usefullness of structures in C#.

    Regards,
    Jeff

    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!

    Comment

    • Jon Skeet [C# MVP]

      #17
      Re: design question : struct or class

      Jeff Louie <anonymous@devd ex.com> wrote:[color=blue]
      > As Jon has stated, objects, by default, are passed by value.[/color]

      I didn't quite state that - I stated that *parameters* are passed by
      value by default. Reference type objects are never passed in any way,
      as the type of an expression is never a reference type object, only a
      reference.

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

      • L#

        #18
        Re: design question : struct or class

        On Fri, 30 Jan 2004 00:56:36 +0100, "codymanix"
        <dont.spam.me.d eutronium@gmx.d e> wrote:

        [color=blue]
        >
        >You should *Always* use classes. structs are only needed in very rare
        >circumatance s which isn't the case.[/color]

        You should use structs if they are very small. They live on the stack
        and are therefore faster. But if they are too big, the advantage of
        being faster dissappears. If they become too big, the framework will
        put them on the heap anyway, even if you declared it as a struct.

        So, is they are very small, use structs, else use classes.

        --
        Ludwig
        mailto:ludwig_( nospamplease)st uyck@pandora(no spamplease).be

        Comment

        • L#

          #19
          Re: design question : struct or class

          On Sat, 31 Jan 2004 12:05:33 -0000, Jon Skeet [C# MVP]
          <skeet@pobox.co m> wrote:
          [color=blue]
          >peter x <lad4bear@boltb lue.com> wrote:
          >
          ><snip>
          >[color=green]
          >> When objects are passed to a method, they are passed by reference.[/color]
          >
          >No they are not. "Pass by reference" has a very specific meaning, and
          >it *doesn't* apply here (unless you use the ref modifier). There is a
          >big difference between a reference being passed by value (which is what
          >actually happens) and a parameter itself being passed *by* reference.
          >
          >See http://www.pobox.com/~skeet/csharp/parameters.html for more
          >information.[/color]

          Being objects, they are passed by value; meaning that the pointer to
          the object is passed by value. Eventually it has the same effect as
          being passed by reference. Placing the ref keyword in front of it,
          won't make a difference.

          --
          Ludwig
          mailto:ludwig_( nospamplease)st uyck@pandora(no spamplease).be

          Comment

          • Joe Mayo [C# MVP]

            #20
            Re: design question : struct or class

            "L#" <ludwig_(nospam please)stuyck@p andora(nospampl ease).be> wrote in message
            news:oo7q10l2rv m4sq3d7oiv3jocc ivjfifbhd@4ax.c om...[color=blue]
            > On Fri, 30 Jan 2004 00:56:36 +0100, "codymanix"
            > <dont.spam.me.d eutronium@gmx.d e> wrote:
            >
            >[color=green]
            > >
            > >You should *Always* use classes. structs are only needed in very rare
            > >circumatance s which isn't the case.[/color]
            >
            > You should use structs if they are very small. They live on the stack
            > and are therefore faster. But if they are too big, the advantage of
            > being faster dissappears. If they become too big, the framework will
            > put them on the heap anyway, even if you declared it as a struct.
            >
            > So, is they are very small, use structs, else use classes.[/color]


            Hi L#,

            Structs reside on the stack if they are parameters or local variables,
            otherwise they are stored on the heap. The best way to determine whether
            the size of the struct is affecting performance is to benchmark your
            application. I've never seen or heard anything that would make me believe
            that the size of a value type determines whether they reside on the stack or
            on the heap.

            Jon Skeet wrote a nice article that explains this better:
            http://www.yoda.arachsys.com/csharp/memory.html.

            The reason you would use a struct instead of a class is if you need
            value-type semantics, similar to int, float, or DateTime.

            Joe
            --
            Welcome to C# Station!  This is a community site for people interested in applying .NET using the C# programming language.  We’ve been around since July 4th 2000 and have continued to grow over the years.  Items of interest include Articles, Books, Links, Documentation,  and Tutorials. More… Source Code If you would like to see an […]



            Comment

            • L#

              #21
              Re: design question : struct or class

              On Sun, 1 Feb 2004 11:26:10 -0700, "Joe Mayo [C# MVP]"
              <jmayo@nospamAt CSharpDashStati on.com> wrote:

              [color=blue]
              >application. I've never seen or heard anything that would make me believe
              >that the size of a value type determines whether they reside on the stack or
              >on the heap.[/color]

              Mmm, indeed, I thought I read somewhere that the framework checks
              this, but apparently it doesn't, because using large structs has a
              negative effect in perfomance.

              [Source:


              We learn, every day.

              --
              Ludwig
              mailto:ludwig_( nospamplease)st uyck@pandora(no spamplease).be

              Comment

              • peter x

                #22
                Re: design question : struct or class

                Enjoyed the article - thanks for pointing it out :) Peter


                "L#" <ludwig_(nospam please)stuyck@p andora(nospampl ease).be> wrote in message
                news:0fqq105h8a 9asjeqqkbcg3rk5 j2dci83o1@4ax.c om...[color=blue]
                > On Sun, 1 Feb 2004 11:26:10 -0700, "Joe Mayo [C# MVP]"
                > <jmayo@nospamAt CSharpDashStati on.com> wrote:
                >
                >[color=green]
                > >application. I've never seen or heard anything that would make me[/color][/color]
                believe[color=blue][color=green]
                > >that the size of a value type determines whether they reside on the stack[/color][/color]
                or[color=blue][color=green]
                > >on the heap.[/color]
                >
                > Mmm, indeed, I thought I read somewhere that the framework checks
                > this, but apparently it doesn't, because using large structs has a
                > negative effect in perfomance.
                >
                > [Source:
                >[/color]
                http://msdn.microsoft.com/library/de...assStruct.asp][color=blue]
                >
                > We learn, every day.
                >
                > --
                > Ludwig
                > mailto:ludwig_( nospamplease)st uyck@pandora(no spamplease).be[/color]


                Comment

                • Jon Skeet [C# MVP]

                  #23
                  Re: design question : struct or class

                  <L# <ludwig_(nospam please)stuyck@p andora(nospampl ease).be>> wrote:[color=blue][color=green]
                  > >No they are not. "Pass by reference" has a very specific meaning, and
                  > >it *doesn't* apply here (unless you use the ref modifier). There is a
                  > >big difference between a reference being passed by value (which is what
                  > >actually happens) and a parameter itself being passed *by* reference.
                  > >
                  > >See http://www.pobox.com/~skeet/csharp/parameters.html for more
                  > >information.[/color]
                  >
                  > Being objects, they are passed by value; meaning that the pointer to
                  > the object is passed by value.[/color]

                  Passing an object doesn't happen in .NET. Only passing a reference
                  happens - and as you say, it's passed by value by default in C#.
                  [color=blue]
                  > Eventually it has the same effect as
                  > being passed by reference.[/color]

                  No it doesn't.
                  [color=blue]
                  > Placing the ref keyword in front of it, won't make a difference.[/color]

                  Yes it will. Here's a sample program:

                  using System;

                  public class Test
                  {
                  static void Main()
                  {
                  string x = "hello";
                  PassByValue (x);
                  Console.WriteLi ne (x);
                  PassByRef (ref x);
                  Console.WriteLi ne (x);
                  }

                  static void PassByValue (string y)
                  {
                  y = "there";
                  }

                  static void PassByRef (ref string y)
                  {
                  y = "there";
                  }
                  }

                  PassByValue and PassByRef are identical apart from the way in which
                  their parameters are passed - yet they have completely different
                  effects.

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

                  • L#

                    #24
                    Re: design question : struct or class

                    On Mon, 2 Feb 2004 08:06:35 -0000, Jon Skeet [C# MVP]
                    <skeet@pobox.co m> wrote:

                    [color=blue][color=green]
                    >> Placing the ref keyword in front of it, won't make a difference.[/color]
                    >
                    >Yes it will. Here's a sample program:
                    >[/color]

                    But doesn't String behave completely different than other Objects?


                    --
                    Ludwig
                    mailto:ludwig_( nospamplease)st uyck@pandora(no spamplease).be

                    Comment

                    • Jon Skeet [C# MVP]

                      #25
                      Re: design question : struct or class

                      <L# <ludwig_(nospam please)stuyck@p andora(nospampl ease).be>> wrote:[color=blue][color=green][color=darkred]
                      > >> Placing the ref keyword in front of it, won't make a difference.[/color]
                      > >
                      > >Yes it will. Here's a sample program:[/color]
                      >
                      > But doesn't String behave completely different than other Objects?[/color]

                      No. What makes you say it does?

                      If you want an example which doesn't use strings at all, here it is:

                      using System;

                      public class Test
                      {
                      int number;

                      public int Number
                      {
                      get { return number; }
                      }

                      Test (int number)
                      {
                      this.number = number;
                      }

                      static void Main()
                      {
                      Test t = new Test(10);
                      PassByValue(t);
                      Console.WriteLi ne (t.Number);
                      PassByRef(ref t);
                      Console.WriteLi ne (t.Number);
                      }

                      static void PassByValue (Test t)
                      {
                      t = new Test(20);
                      }

                      static void PassByRef (ref Test t)
                      {
                      t = new Test(20);
                      }
                      }

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

                      • L#

                        #26
                        Re: design question : struct or class

                        On Mon, 2 Feb 2004 11:21:25 -0000, Jon Skeet [C# MVP]
                        <skeet@pobox.co m> wrote:
                        [color=blue]
                        >
                        >No. What makes you say it does?
                        >
                        >If you want an example which doesn't use strings at all, here it is:
                        >[/color]

                        So if you modify the object, there's no difference in passing by
                        value/passing by reference; but if you create a new object, there's a
                        difference. Didn't know that!


                        --
                        Ludwig
                        mailto:ludwig_( nospamplease)st uyck@pandora(no spamplease).be

                        Comment

                        • Jon Skeet [C# MVP]

                          #27
                          Re: design question : struct or class

                          <L# <ludwig_(nospam please)stuyck@p andora(nospampl ease).be>> wrote:[color=blue]
                          > So if you modify the object, there's no difference in passing by
                          > value/passing by reference; but if you create a new object, there's a
                          > difference. Didn't know that![/color]

                          The important difference is that modifying the object a parameter's
                          value refers to doesn't actually change the value of the parameter.
                          Changing the value of the parameter (e.g. by creating a new object, but
                          that's only one type of new value) is the important thing, and that's
                          where the difference between pass-by-reference and pass-by-value
                          semantics lies.

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

                          Working...