struct or class

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

    #1

    struct or class

    Hello!

    Since struct and class are very similar in C#, I was wondering why using
    struct would be better in some situations than using class. Any performance
    issues? What about sending struct or class as parameter to function? Is it
    by value or reference?

    Appreciate any thoughts on this issue!


    Best regards,
    Jure


  • Marc Gravell

    #2
    Re: struct or class

    Since struct and class are very similar in C#
    No, struct and class are *very* different in C#; much more so than in
    C++.

    Jon's pages give a good overview:
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    I was wondering why using struct would be better in some situations
    than using class
    Struct is value-type; class is reference-type. I'd use "different"
    more than "better", but yes - in some scenarios it can be marginally
    quicker to use a struct, as there is no indirection step to the
    managed heap. However, an overweight struct (as an argument) will
    typically make things worse rather than better, as the struct is
    (normally) cloned (simple memcopy).
    What about sending struct or class as parameter to function? Is it
    by value or reference?
    The type can be value-type of reference-type, and *separately*, either
    can be passed by-reference or by-value. All 4 options are possible.

    But read Jon's article.

    Marc


    Comment

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

      #3
      RE: struct or class

      Read this
      C# Struct, A structure in C# is simply a composite data type consisting of a number elements of other types.


      --
      Sincerely
      Yaron Karni
      All .net c# stuff, patterns code and snippet help programmer.



      "Jure Bogataj" wrote:
      Hello!
      >
      Since struct and class are very similar in C#, I was wondering why using
      struct would be better in some situations than using class. Any performance
      issues? What about sending struct or class as parameter to function? Is it
      by value or reference?
      >
      Appreciate any thoughts on this issue!
      >
      >
      Best regards,
      Jure
      >
      >
      >

      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: struct or class

        On Aug 28, 10:30 am, Yaron Karni <yaron.ka...@gm ail.comwrote:Unfortunately it falls down on the second sentence:

        <quote>
        A C# structure is a value type and the instances or objects of a
        structure are created in stack.
        </quote>

        Structs are sometimes created on the stack, but if they're part of
        other classes, they'll be on the heap.

        See http://pobox.com/~skeet/csharp/memory.html

        Jon

        Comment

        • Mark Rae [MVP]

          #5
          Re: struct or class

          "Jure Bogataj" <jure.bogataj@m ikrocop.comwrot e in message
          news:ukLIW1U6HH A.1168@TK2MSFTN GP02.phx.gbl...
          Since struct and class are very similar in C#
          In what way(s) are they "very similar"...???


          --
          Mark Rae
          ASP.NET MVP


          Comment

          • Jure Bogataj

            #6
            Re: struct or class

            Based on that structures can contain many same elements that classes do
            (constructors, constants, fields, methods, properties, indexers, operators,
            events, and nested types). But I do not have some deeper knowledge about
            their memory considerations, layout or usage so I would appreciate any
            thoughts or links for that. So to know when to use struct and when to use
            class.
            For example, I have made a function in C# which accepts structure as input
            parameter (structure contains fields for some search filter; I could also
            write all parameters inside function, but I rather created structure for
            this). Then I've heard that it is better to use class (and that structures
            are only usable in an unmanaged code), and didn't quite understand why.


            Best regards,
            Jure


            "Mark Rae [MVP]" <mark@markNOSPA Mrae.netwrote in message
            news:OgdwMdV6HH A.2208@TK2MSFTN GP06.phx.gbl...
            "Jure Bogataj" <jure.bogataj@m ikrocop.comwrot e in message
            news:ukLIW1U6HH A.1168@TK2MSFTN GP02.phx.gbl...
            >
            >Since struct and class are very similar in C#
            >
            In what way(s) are they "very similar"...???
            >
            >
            --
            Mark Rae
            ASP.NET MVP
            http://www.markrae.net

            Comment

            • Marc Gravell

              #7
              Re: struct or class

              If in doubt (and it sounds like you are), then you should almost
              always be using a class. It is relatively unusual to write structs in
              ..NET - mainly to represent composite values, such as a complex-number,
              or a currency/value pair - that type of thing.

              You can always make the class immutable if you want value-type-like
              semantics - i.e. it can't be edited after being created.

              Marc


              Comment

              • =?Utf-8?B?RGF2aWQgQW50b24=?=

                #8
                Re: struct or class

                This is a common source of confusion for C++ developers, since the difference
                between struct and class in C++ is miniscule (and you could easily argue that
                retaining struct was a mistake in the design of C++).
                --
                David Anton
                Source code converters: Convert between C#, C++, Java, and VB with the most accurate and reliable source code converters

                Convert between VB, C#, C++, and Java
                Instant C#
                Instant VB
                Instant C++
                C++ to C# Converter
                C++ to VB Converter
                C++ to Java Converter


                "Marc Gravell" wrote:
                Since struct and class are very similar in C#
                No, struct and class are *very* different in C#; much more so than in
                C++.
                >
                Jon's pages give a good overview:
                Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                >
                I was wondering why using struct would be better in some situations
                than using class
                Struct is value-type; class is reference-type. I'd use "different"
                more than "better", but yes - in some scenarios it can be marginally
                quicker to use a struct, as there is no indirection step to the
                managed heap. However, an overweight struct (as an argument) will
                typically make things worse rather than better, as the struct is
                (normally) cloned (simple memcopy).
                >
                What about sending struct or class as parameter to function? Is it
                by value or reference?
                The type can be value-type of reference-type, and *separately*, either
                can be passed by-reference or by-value. All 4 options are possible.
                >
                But read Jon's article.
                >
                Marc
                >
                >
                >

                Comment

                • Ben Voigt [C++ MVP]

                  #9
                  Re: struct or class


                  "Marc Gravell" <marc.gravell@g mail.comwrote in message
                  news:OLFkK8U6HH A.1148@TK2MSFTN GP05.phx.gbl...
                  >Since struct and class are very similar in C#
                  No, struct and class are *very* different in C#; much more so than in C++.
                  >
                  Jon's pages give a good overview:
                  Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                  >
                  >I was wondering why using struct would be better in some situations than
                  >using class
                  Struct is value-type; class is reference-type. I'd use "different" more
                  than "better", but yes - in some scenarios it can be marginally quicker to
                  use a struct, as there is no indirection step to the managed heap.
                  However, an overweight struct (as an argument) will typically make things
                  worse rather than better, as the struct is (normally) cloned (simple
                  memcopy).
                  When using an array, a struct can be much much much better, as you save the
                  indirection and also you have perfect locality improving the performance of
                  the CPU cache. Also the garbage collection cost depends on the number of
                  live objects and the number of tracking handles, and by using an array of
                  struct you save both.


                  Comment

                  • Marc Gravell

                    #10
                    Re: struct or class

                    When using an array, a struct can be much much much better
                    Fair enough - but you'd have to be doing some heavy crunching to
                    really appreciate this (so maybe a math/graphical/financial
                    application). And of course, in such cases you'd be advised look at
                    the profiler first - it could still be that this is the 0.5%, and
                    you'd be better focussing on something that is sucking 40% CPU.

                    But I take your point.

                    Marc

                    Comment

                    Working...