class or struct
-
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 -
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 111111Depends 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 Thomaswww.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. -
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 111111if you consider using anything else but value types , then use a class. If not, you're free to use a struct
-
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 111111Another 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.
-
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 111111Unless 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.
-
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.
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.
-
if you consider using anything else but value types , then use a class. If not, you're free to use a struct
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. -
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.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
-
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
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. -
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 111111If 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.