473,394 Members | 1,889 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Struct vs. Class

If my intentions are to create objects that encapsulates data rows in a
table, is it better to use a Struct or Class? Based on what i read, if my
objects will simply have get and set methods, Struct is may be better...but i
am looking for some advise from the experts on this?
Assumptions: it is possible for some operations, i may have 8000 to 10000
(or more) objects instantiated.

what are some considerations in designing a system that may instantiate this
many objects? what sort of performance advantage would Structs give me over
Classes?

thank you
Nov 16 '05 #1
11 2209
dimension wrote:
If my intentions are to create objects that encapsulates data rows in a
table, is it better to use a Struct or Class? Based on what i read, if my
objects will simply have get and set methods, Struct is may be better...but i
am looking for some advise from the experts on this?
Assumptions: it is possible for some operations, i may have 8000 to 10000
(or more) objects instantiated.
ONLY use structs for complex value types, like an int with special
characteristics. Structs are value types, not objects, so as soon as
you're using them as objects, you'll run into issues. For example, if
you index into an arraylist and do this:
((MyStructType)myArrayList[index]).Property = value;

if at that spot a struct is located, the indexer will return a copy, as
it is a value type (If I recall correctly, C# will not even compile the
above line).

So rule of thumb: almost always use classes, not structs.
what are some considerations in designing a system that may instantiate this
many objects? what sort of performance advantage would Structs give me over
Classes?


none.

FB

--
Get LLBLGen Pro, productive O/R mapping for .NET: http://www.llblgen.com
My .NET Blog : http://weblogs.asp.net/FBouma
Microsoft MVP (C#)
Nov 16 '05 #2
If you plan to provide access to the returned data via an object for each
row, it might be better to only create objects that are actually needed at
the time. You could hold the dataset in memory and have some factory class
that you can call which will create and return an object for a particular
row.

"dimension" <di*******@discussions.microsoft.com> wrote in message
news:FA**********************************@microsof t.com...
If my intentions are to create objects that encapsulates data rows in a
table, is it better to use a Struct or Class? Based on what i read, if my
objects will simply have get and set methods, Struct is may be
better...but i
am looking for some advise from the experts on this?
Assumptions: it is possible for some operations, i may have 8000 to 10000
(or more) objects instantiated.

what are some considerations in designing a system that may instantiate
this
many objects? what sort of performance advantage would Structs give me
over
Classes?

thank you

Nov 16 '05 #3
See
http://msdn.microsoft.com/library/de...guidelines.asp

Contrary top previous comments, structs can provide performance enhancements
*if used correctly*. They are allocated on the stack, not heap, and so do not
need to be managed by the GC.

Performance gains really depend on how the data will be used.

Hope this helps
Dan

"dimension" wrote:
If my intentions are to create objects that encapsulates data rows in a
table, is it better to use a Struct or Class? Based on what i read, if my
objects will simply have get and set methods, Struct is may be better...but i
am looking for some advise from the experts on this?
Assumptions: it is possible for some operations, i may have 8000 to 10000
(or more) objects instantiated.

what are some considerations in designing a system that may instantiate this
many objects? what sort of performance advantage would Structs give me over
Classes?

thank you

Nov 16 '05 #4
Dan Kelley <Da*******@discussions.microsoft.com> wrote:
See
http://msdn.microsoft.com/library/de...ary/en-us/cpge
nref/html/cpconvaluetypeusageguidelines.asp

Contrary top previous comments, structs can provide performance enhancements
*if used correctly*. They are allocated on the stack, not heap, and so do not
need to be managed by the GC.


They are allocated on the stack in *some* situations. If they're member
variables of an object on the heap, or static variables, or elements of
an array, or boxed, they're on the heap.

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

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Dan Kelley <Da*******@discussions.microsoft.com> wrote:
See
http://msdn.microsoft.com/library/de...ary/en-us/cpge
nref/html/cpconvaluetypeusageguidelines.asp

Contrary top previous comments, structs can provide performance
enhancements
*if used correctly*. They are allocated on the stack, not heap, and so do
not
need to be managed by the GC.


They are allocated on the stack in *some* situations. If they're member
variables of an object on the heap, or static variables, or elements of
an array, or boxed, they're on the heap.

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


And even then, they are only completely allocated on the stack if they are
composed of only value types.
Consider this:
struct st
{
public int i;
public string s; // object type - non blitable
}

void MyMethod()
{
st val; //local variable val
val.s = "test"; // the object representing the string is on the heap
...

Here val is on the stack and looks like:

address value
0x0012f600 00aa2f00 - this is a reference to the string type on the GC
heap
0x0012f604 00000000 - this is the actual value of i

address 0x0012f604 is the stack location of val.

Willy.

Nov 16 '05 #6
Some basic rules...

Don't use structs until you know exactly why NOT to use them...
Don't use threads until you know exactly why NOT to use them...
Don't use ...

Everybody that used threads a lot can tell you that you are better of
without. (This doesn't mean you shouldn't use them...) Same for structs,
it is'nt worth the effort.

10.000 objects... each object 32 bytes (32 bytes is an average...) ==> 312
KB. Not even half a MB.
Walk in the park...

kind regards

Alexander



"dimension" <di*******@discussions.microsoft.com> wrote in message
news:FA**********************************@microsof t.com...
If my intentions are to create objects that encapsulates data rows in a
table, is it better to use a Struct or Class? Based on what i read, if my
objects will simply have get and set methods, Struct is may be
better...but i
am looking for some advise from the experts on this?
Assumptions: it is possible for some operations, i may have 8000 to 10000
(or more) objects instantiated.

what are some considerations in designing a system that may instantiate
this
many objects? what sort of performance advantage would Structs give me
over
Classes?

thank you

Nov 16 '05 #7
> Alexander Muylaertwrote:
Some basic rules...

Don't use structs until you know exactly why NOT to use them...
Don't use threads until you know exactly why NOT to use them...
Don't use ...


Are you saying that if you don't know to NOT use them, then you can
use them?

You shouldn't use threads unless you understand how they work, how
concurrency works, and the issues that you will run into with them.
It isn't a matter of knowing exactly why NOT to use them, it's a
matter of knowing HOW to use them, when, and why. If you don't know
how, then don't use them.

You shouldn't use structs if you think of them as objects. In most
cases, it's best just to use classes. My opinion is that structs are
a throwback to C/C++, and are there to appease the converts from them.

*-----------------------*
Posted at:
www.GroupSrv.com
*-----------------------*
Nov 16 '05 #8
Thanks all for your feedback.
My concern is performance. So let's take this approach...

assume i don't know a thing about Structs and used them improperly, would
the worst case scenario be that i end up with basically a memory footprint
similar to a class? Are accessing methods or and properties more efficient
for one verses the other? (I know in most cases this would not make a
difference...but assuming i need to do mass processing of 10000 objects)

btw, i like the suggestion by one responder with regard to having a Factory
object keep a DataSet/Datatable in memory and returning class objects or
struct types only when needed. great idea and actually would reduce round
trips to the database, as it would be sort of like a caching mechanism.
thanks!

"Molybedenum" wrote:
Alexander Muylaertwrote:

Some basic rules...

Don't use structs until you know exactly why NOT to use them...
Don't use threads until you know exactly why NOT to use them...
Don't use ...


Are you saying that if you don't know to NOT use them, then you can
use them?

You shouldn't use threads unless you understand how they work, how
concurrency works, and the issues that you will run into with them.
It isn't a matter of knowing exactly why NOT to use them, it's a
matter of knowing HOW to use them, when, and why. If you don't know
how, then don't use them.

You shouldn't use structs if you think of them as objects. In most
cases, it's best just to use classes. My opinion is that structs are
a throwback to C/C++, and are there to appease the converts from them.

*-----------------------*
Posted at:
www.GroupSrv.com
*-----------------------*

Nov 16 '05 #9
Oops - thought all that would be fairly obvious :) My mistake for not posting
a complete reply. Thanks for clarifying things for the OP.

Dan

"Willy Denoyette [MVP]" wrote:

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Dan Kelley <Da*******@discussions.microsoft.com> wrote:
See
http://msdn.microsoft.com/library/de...ary/en-us/cpge
nref/html/cpconvaluetypeusageguidelines.asp

Contrary top previous comments, structs can provide performance
enhancements
*if used correctly*. They are allocated on the stack, not heap, and so do
not
need to be managed by the GC.


They are allocated on the stack in *some* situations. If they're member
variables of an object on the heap, or static variables, or elements of
an array, or boxed, they're on the heap.

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


And even then, they are only completely allocated on the stack if they are
composed of only value types.
Consider this:
struct st
{
public int i;
public string s; // object type - non blitable
}

void MyMethod()
{
st val; //local variable val
val.s = "test"; // the object representing the string is on the heap
...

Here val is on the stack and looks like:

address value
0x0012f600 00aa2f00 - this is a reference to the string type on the GC
heap
0x0012f604 00000000 - this is the actual value of i

address 0x0012f604 is the stack location of val.

Willy.

Nov 16 '05 #10
dimension <di*******@discussions.microsoft.com> wrote in message news:<97**********************************@microso ft.com>...
Thanks all for your feedback.
My concern is performance. So let's take this approach...

assume i don't know a thing about Structs and used them improperly, would
the worst case scenario be that i end up with basically a memory footprint
similar to a class? Are accessing methods or and properties more efficient
for one verses the other? (I know in most cases this would not make a
difference...but assuming i need to do mass processing of 10000 objects)

btw, i like the suggestion by one responder with regard to having a Factory
object keep a DataSet/Datatable in memory and returning class objects or
struct types only when needed. great idea and actually would reduce round
trips to the database, as it would be sort of like a caching mechanism.
thanks!


In answer to your specific question, if you use structs incorrectly
you can produce absolutely horrible performance. Remember that every
time you assign a struct to a variable or pass it to a method you
create a copy. Sometimes this is exactly the behaviour you want: you
want your structure to act like an elementary type like an integer or
a decimal type. For example, I created a struct called Fraction that
has three integer fields: WholePart, Numerator, and Denominator. I
made it a struct because I want it to act exactly like an integer or a
decimal: assigning a fraction to a Fraction variable makes a copy
rather than having two variables share a reference to the same
fraction. However, the penalty I pay for this behaviour is that every
time I pass a Fraction anywhere or do any mathematics with it, I copy
three integers.

Classes behave differently. Whenever you assign one class variable to
another class variable (called reference variables), you simple copy a
reference (a pointer) from one to the other, but they now share the
same object.

There are fundamental differences between value semantics and
reference semantics in .NET. IMHO, you should not choose one over the
other because one is "more efficient". You should choose to use
structs rather than classes because your design calls for value
behaviour. You should choose classes over structs because your design
calls for reference behaviour (which is almost always the case). I ask
myself, "Is this thing I'm building like a basic value, such as an
integer or a double?" If the answer is no, then it's a class; if the
answer is yes, the it's s struct.

I don't presume to speak for programmers writing software for true
real-time systems where every clock cycle counts, although, as I've
said in other groups, if you're writing for that kind of tight
environment then I question why you're using a language with a garbage
collector, but anyway.... My take on making these decisions based on
"efficiency" is that you should do this only if you must, and only
then if you have a deep understanding of the consequences of choosing
structs over classes (value semantics over reference semantics), or
vice versa.

You can gain far more performance improvement by using appropriate
data structures and improving your overall design than you can by
making tweaks like choosing structs over classes. If you build your
application and find that it doesn't perform well, THEN profile it and
figure out where the bottlenecks are. Creating a big software mess
trying to shoehorn structs into an application in the name of
"efficiency" is generally not productive and can, as I pointed out
above, jackpot you in the end with inefficiencies that you weren't
counting on.

Design for clarity, then use the constructs that logically follow from
your design. You should deal with small-scale efficiency problems
after your application is running and you know where the problem areas
are, otherwise you're just firing blind.
Nov 16 '05 #11
dimension <di*******@discussions.microsoft.com> wrote in message news:<FA**********************************@microso ft.com>...
If my intentions are to create objects that encapsulates data rows in a
table, is it better to use a Struct or Class? Based on what i read, if my
objects will simply have get and set methods, Struct is may be better...but i
am looking for some advise from the experts on this?
Assumptions: it is possible for some operations, i may have 8000 to 10000
(or more) objects instantiated.

what are some considerations in designing a system that may instantiate this
many objects? what sort of performance advantage would Structs give me over
Classes?

thank you


Looking back at your original post, I see that you're creating
business objects, presumably that encapsulate business rules and get
their data from ADO.NET data rows. This is exactly what I'm doing:
each row becomes an object with the smarts to know what the data
means, the objects are used in the application, altered, and then
written back to the database through the appropriate data rows.

If this is, in fact, what you are doing, I can state unequivocally
100% that you do not want these things to be structs. If you attempt
to make them structs you will run into all sorts of problems, such as
wondering why when you put them in an aggregate structure and try to
change them they don't change:

MyStruct s = (MyStruct)myHashTable[key];
s.Description = "New description";

then next time you look up myHashTable[key] you find that it still
contains the old description.

Stuff like that that will leave you scratching your head until you
finally realize that no, in fact these things shouldn't be structs,
they should be classes... and then you have to redo the whole project.

As well, if you're building objects as I described, then consider the
following. You are reading all of these objects from a database into
ADO.NET data tables. The operation of going to a database, executing a
SQL query, creating appropriate ADO.NET tables, and populating them
with data is so outrageously expensive compared to the cost--both in
terms of cycles and memory--involved in working with either structs or
classes at the end of it all that choosing one construct over the
other will make absolutely no noticeable performance difference. None.
Squat. ADO.NET uses relatively large amounts of memory, and executing
database queries, particularly over networks, comes with such a
serious performance penalty that anything you're doing locally in your
own CPU and your own memory likely pales in comparison.

If you choose one construct over the other for "performance" reasons,
and actually manage to choose correctly (which would be beyond my ken,
at least), then you may be able to shave a millisecond off an
operation than then heads off into the bowels of ADO.NET for a second
or two to fetch data or write it back across the network into the
database.

Instead of trying to shave a millisecond off your execution time, or
save 10K of memory, you would be much better off optimizing your
database access or being careful in your use of ADO.NET to save
seconds of execution time and hundreds of K of memory.

As I said: build it first according to solid design principles. Then
figure out where it's slow / a memory pig using a profiler, and
concentrate on those areas.
Nov 16 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Kilana | last post by:
I see this all the time in code: typedef struct a_struct { ... }differentName, *differentNamePtr; I understand how I can use it, but could someone tell me why the above is
2
by: SACHIN | last post by:
I have this class as part of a Consol application. using System; namespace Bugreport { /// <summary> /// This class tries to use the Class/Struct combination. /// </summary> class Class1 {
4
by: Steve | last post by:
I'll be the first to admit, I'm not entirely clear on the appropriate usage of either. From what I am reading in my books, a Struct and a Class are pretty much the same, with the difference being,...
15
by: Steven T. Hatton | last post by:
The following may strike many of you as just plain silly, but it represents the kind of delelima I find myself in when trying to make a design decision. This really is a toy project written for...
15
by: bugzilla | last post by:
hi,all, I have a C++ program need to convert to c language to be used in a emabedded system. the problem is that the original code was writtern in C++ language with Parent class and some child...
3
by: Karl M | last post by:
Hi everyone, I just notice some strange behaviors on the MS C++ compiler regarding struct default constructor, see the example bellow: struct MyStruct { int a; }; class MyClass { public:
4
by: DaHool | last post by:
Hi there !!! I browsed around the Internet in search for a solution of a little difficult problem i have in VB.NET.... However, i cannot find a suitable anwser anywhere, so i thought i'll give...
5
by: jwright | last post by:
I have decided to use a struct to collect my data. The input file is comma dilineated between almost all of the fields. Here is the code I have so far and a sample input and output file. ...
1
by: stromhau | last post by:
Hi, I have made a few classes in c++. They somehow cooperate doing some 3d stuff. Basically it is a moving camera acting as a flight, i have placed a lot of objects around the scene together with...
2
by: Ninereeds | last post by:
I'm messing around with using mixin-layers (look for papers by Yannis Smaragdakis and Don Batory) to define data structures. One issue is that nodes tend to have pointers to other nodes - the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.