Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. class or struct

class or struct

Scheduled Pinned Locked Moved C#
csharpquestiondatabase
10 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    Harvey Saayman
    wrote on last edited by
    #1

    Hey guys I need to create an object for a calculation procedure I'm busy writing that will contain two DateTime values... Now for the million dollar question.... Do I create a class or struct?? Any why? I remember talking about this with leppie before but cant remember when or what exactly the thread was on... :sigh: so would you please enlighten me once more bud :) i promise to take notes this time :-D Thanks

    Harvey Saayman - South Africa Junior Developer .Net, C#, SQL you.suck = (you.Passion != Programming & you.Occupation == jobTitles.Programmer) 1000100 1101111 1100101 1110011 100000 1110100 1101000 1101001 1110011 100000 1101101 1100101 1100001 1101110 100000 1101001 1101101 100000 1100001 100000 1100111 1100101 1100101 1101011 111111

    T J A G P 5 Replies Last reply
    0
    • H Harvey Saayman

      Hey guys I need to create an object for a calculation procedure I'm busy writing that will contain two DateTime values... Now for the million dollar question.... Do I create a class or struct?? Any why? I remember talking about this with leppie before but cant remember when or what exactly the thread was on... :sigh: so would you please enlighten me once more bud :) i promise to take notes this time :-D Thanks

      Harvey Saayman - South Africa Junior Developer .Net, C#, SQL you.suck = (you.Passion != Programming & you.Occupation == jobTitles.Programmer) 1000100 1101111 1100101 1110011 100000 1110100 1101000 1101001 1110011 100000 1101101 1100101 1100001 1101110 100000 1101001 1101101 100000 1100001 100000 1100111 1100101 1100101 1101011 111111

      T Offline
      T Offline
      Thomas Weller 0
      wrote on last edited by
      #2

      Depends on whether you have to move the class/object around as an argument. If so, use a class. Otherwise, it doesn't really matter technically, the two DateTimes will live equally happy on the heap or on the stack. But normally, structs are expected to be read-only data containers without behaviour (i.e. they should have no methods except c'tors, the 3 object-inherited methods, and operator-like methods like e.g. Compare). What you have in mind sounds more like a service, so I think a class would be the better choice. Regards Thomas

      www.thomas-weller.de Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
      Programmer - an organism that turns coffee into software.

      1 Reply Last reply
      0
      • H Harvey Saayman

        Hey guys I need to create an object for a calculation procedure I'm busy writing that will contain two DateTime values... Now for the million dollar question.... Do I create a class or struct?? Any why? I remember talking about this with leppie before but cant remember when or what exactly the thread was on... :sigh: so would you please enlighten me once more bud :) i promise to take notes this time :-D Thanks

        Harvey Saayman - South Africa Junior Developer .Net, C#, SQL you.suck = (you.Passion != Programming & you.Occupation == jobTitles.Programmer) 1000100 1101111 1100101 1110011 100000 1110100 1101000 1101001 1110011 100000 1101101 1100101 1100001 1101110 100000 1101001 1101101 100000 1100001 100000 1100111 1100101 1100101 1101011 111111

        J Offline
        J Offline
        Jan Sommer
        wrote on last edited by
        #3

        if you consider using anything else but value types , then use a class. If not, you're free to use a struct

        T 1 Reply Last reply
        0
        • H Harvey Saayman

          Hey guys I need to create an object for a calculation procedure I'm busy writing that will contain two DateTime values... Now for the million dollar question.... Do I create a class or struct?? Any why? I remember talking about this with leppie before but cant remember when or what exactly the thread was on... :sigh: so would you please enlighten me once more bud :) i promise to take notes this time :-D Thanks

          Harvey Saayman - South Africa Junior Developer .Net, C#, SQL you.suck = (you.Passion != Programming & you.Occupation == jobTitles.Programmer) 1000100 1101111 1100101 1110011 100000 1110100 1101000 1101001 1110011 100000 1101101 1100101 1100001 1101110 100000 1101001 1101101 100000 1100001 100000 1100111 1100101 1100101 1101011 111111

          A Offline
          A Offline
          Alan Balkany
          wrote on last edited by
          #4

          Another minor difference: A struct is accessed by a pointer to its start in memory, but a class is accessed by a pointer to a pointer to its start. So if you're repeatedly accessing this object and you want the maximum efficiency, a struct will save one memory read per access. As was pointed out earlier, a struct is less efficient to pass as a parameter because the whole object is pushed onto the stack. A class only has its reference (an address) put on the stack, so is faster to pass.

          G 1 Reply Last reply
          0
          • H Harvey Saayman

            Hey guys I need to create an object for a calculation procedure I'm busy writing that will contain two DateTime values... Now for the million dollar question.... Do I create a class or struct?? Any why? I remember talking about this with leppie before but cant remember when or what exactly the thread was on... :sigh: so would you please enlighten me once more bud :) i promise to take notes this time :-D Thanks

            Harvey Saayman - South Africa Junior Developer .Net, C#, SQL you.suck = (you.Passion != Programming & you.Occupation == jobTitles.Programmer) 1000100 1101111 1100101 1110011 100000 1110100 1101000 1101001 1110011 100000 1101101 1100101 1100001 1101110 100000 1101001 1101101 100000 1100001 100000 1100111 1100101 1100101 1101011 111111

            G Offline
            G Offline
            Guffa
            wrote on last edited by
            #5

            Unless you have a special reason to make a struct, make it a class. Classes are easy to implement, just put a few properties in it and it works without problems. A struct needs more work and more knowledge to be implemented properly, or you may end up with a struct that does unexpected things.

            Despite everything, the person most likely to be fooling you next is yourself.

            1 Reply Last reply
            0
            • A Alan Balkany

              Another minor difference: A struct is accessed by a pointer to its start in memory, but a class is accessed by a pointer to a pointer to its start. So if you're repeatedly accessing this object and you want the maximum efficiency, a struct will save one memory read per access. As was pointed out earlier, a struct is less efficient to pass as a parameter because the whole object is pushed onto the stack. A class only has its reference (an address) put on the stack, so is faster to pass.

              G Offline
              G Offline
              Guffa
              wrote on last edited by
              #6

              Alan Balkany wrote:

              As was pointed out earlier, a struct is less efficient to pass as a parameter because the whole object is pushed onto the stack. A class only has its reference (an address) put on the stack, so is faster to pass.

              If the struct is small enough, it can be copied with a single instruction, making it just as efficient as copying a reference. It's recommended that a struct should not be larger than 16 bytes, so that it can be copied with a few instructions, without using a loop.

              Despite everything, the person most likely to be fooling you next is yourself.

              1 Reply Last reply
              0
              • J Jan Sommer

                if you consider using anything else but value types , then use a class. If not, you're free to use a struct

                T Offline
                T Offline
                Thomas Weller 0
                wrote on last edited by
                #7

                Jan Sommer wrote:

                if you consider using anything else but value types

                Why should this make any difference? Can you explain that? The struct/class may live on the stack/heap. In any case, it will contain its fields, so they will consequently live on the stack/heap as well. It is therefore absolutely irrelevant if they were values or refs. Regards Thomas

                www.thomas-weller.de Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
                Programmer - an organism that turns coffee into software.

                J 1 Reply Last reply
                0
                • T Thomas Weller 0

                  Jan Sommer wrote:

                  if you consider using anything else but value types

                  Why should this make any difference? Can you explain that? The struct/class may live on the stack/heap. In any case, it will contain its fields, so they will consequently live on the stack/heap as well. It is therefore absolutely irrelevant if they were values or refs. Regards Thomas

                  www.thomas-weller.de Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
                  Programmer - an organism that turns coffee into software.

                  J Offline
                  J Offline
                  Jan Sommer
                  wrote on last edited by
                  #8

                  Boxing occurs if you use reference types in structs.. also, if he only plans to use value types it doesn't allocate as much memory then classes... see http://msdn.microsoft.com/en-us/library/ah19swz4(VS.71).aspx for details

                  T 1 Reply Last reply
                  0
                  • J Jan Sommer

                    Boxing occurs if you use reference types in structs.. also, if he only plans to use value types it doesn't allocate as much memory then classes... see http://msdn.microsoft.com/en-us/library/ah19swz4(VS.71).aspx for details

                    T Offline
                    T Offline
                    Thomas Weller 0
                    wrote on last edited by
                    #9

                    Jan Sommer wrote:

                    Boxing occurs if you use reference types in structs..

                    :confused: Ummm, what? No! This would mean that whole graphs of classes would be copied from the heap to the stack eventuallly when referenced by a struct :omg: . See here: http://en.csharp-online.net/Classes,_Structs,_and_Objects—When_Boxing_Occurs[^]

                    Jan Sommer wrote:

                    also, if he only plans to use value types it doesn't allocate as much memory then classes

                    This is also wrong as soon as there is more than one reference to the class/struct. Regards Thomas

                    www.thomas-weller.de Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
                    Programmer - an organism that turns coffee into software.

                    1 Reply Last reply
                    0
                    • H Harvey Saayman

                      Hey guys I need to create an object for a calculation procedure I'm busy writing that will contain two DateTime values... Now for the million dollar question.... Do I create a class or struct?? Any why? I remember talking about this with leppie before but cant remember when or what exactly the thread was on... :sigh: so would you please enlighten me once more bud :) i promise to take notes this time :-D Thanks

                      Harvey Saayman - South Africa Junior Developer .Net, C#, SQL you.suck = (you.Passion != Programming & you.Occupation == jobTitles.Programmer) 1000100 1101111 1100101 1110011 100000 1110100 1101000 1101001 1110011 100000 1101101 1100101 1100001 1101110 100000 1101001 1101101 100000 1100001 100000 1100111 1100101 1100101 1101011 111111

                      P Offline
                      P Offline
                      PIEBALDconsult
                      wrote on last edited by
                      #10

                      If you think of it as a value, make it a struct. If having operators for it makes sense, you're likely thinking of it as a value.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups