473,386 Members | 1,757 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,386 software developers and data experts.

Why are some types implemented as struct?

I know the basic differences between a struct and a class, but
apparently not good enough to know why some types/concepts are
implemented as struct whereas most types are implemented as class.
For example, why is DateTime implemented as a struct? Why is BitArray
implemented as a class, but BitVector32 a struct?

In general, in OO design, how do we determine if we should use struct
or class?
Jul 28 '08 #1
12 1525
MC
>I know the basic differences between a struct and a class, but
apparently not good enough to know why some types/concepts are
implemented as struct whereas most types are implemented as class.
For example, why is DateTime implemented as a struct? Why is BitArray
implemented as a class, but BitVector32 a struct?

In general, in OO design, how do we determine if we should use struct
or class?
I think that, in general, structs are for small things that use a fixed
amount of memory.
Jul 28 '08 #2
On Jul 28, 10:33*am, Author <gnewsgr...@gmail.comwrote:
I know the basic differences between a struct and a class, but
apparently not good enough to know why some types/concepts are
implemented as struct whereas most types are implemented as class.
For example, why is DateTime implemented as a struct? *Why is BitArray
implemented as a class, but BitVector32 a struct?

In general, in OO design, how do we determine if we should use struct
or class?
Struct are placed in memory on the stack, therefore faster and do not
need to be cleaned up with garbage collection. Structs cannot be
extended and cannot extend anything else.

Classes Are on the heap, must be cleaned up and can be extended or
extend.

For most of us, a big "So what?"

I am assuming the choice af struct vs class is something that MS
thinks a lot more about in a framework than the common code grunt and
it comes down to first, performance and then to Symmetry in the code.

thats my take on it.

Tal
Jul 28 '08 #3
Nearly every time I have designed something as a struct I have later
discovered I need to be able to change the values in it and ended up
converting it to a class, so now I just don't bother with them any more.

Jul 28 '08 #4
"Peter Morris" <mr*********@SPAMgmail.comwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
Nearly every time I have designed something as a struct I have later
discovered I need to be able to change the values in it and ended up
converting it to a class, so now I just don't bother with them any more.
You can change values in a struct as easily as you can in a class. In fact,
in a lot of ways, you can make a struct act like a class. You can create
properties that you can get and set, just like a class. You can create
methods, you can make field members public. So I'm not really sure what you
mean.
Jul 28 '08 #5
On Jul 28, 12:23*pm, "MC" <for.address.l...@www.ai.uga.edu.slash.mc>
wrote:
I know the basic differences between a struct and a class, but
apparently not good enough to know why some types/concepts are
implemented as struct whereas most types are implemented as class.
For example, why is DateTime implemented as a struct? *Why is BitArray
implemented as a class, but BitVector32 a struct?
In general, in OO design, how do we determine if we should use struct
or class?

I think that, in general, structs are for small things that use a fixed
amount of memory.
Hmm, sounds reasonable, at least with regard to BitArray (resizable)
and BitVector32 (not resizable). But is this the only possible reason
that some types are implemented as struct and others class?

I've read the other responses of my question, but they seem to be off-
topic. I would like to hear some guruish comment about my question.
Jul 28 '08 #6
On Mon, 28 Jul 2008 11:53:40 -0700, Fredo <fr***@hotmail.comwrote:
"Peter Morris" <mr*********@SPAMgmail.comwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
>Nearly every time I have designed something as a struct I have later
discovered I need to be able to change the values in it and ended up
converting it to a class, so now I just don't bother with them any more.

You can change values in a struct as easily as you can in a class.
Well, yes and no.

There are some serious problems with making a struct mutable. The fact
that structs are _copied_ rather than references leads to code that either
doesn't work as expected (you think you've changed an instance but have
only changed a copy of an instance), or just doesn't compile, or is more
inconvenient than it could or should be.

Pete
Jul 28 '08 #7
On Mon, 28 Jul 2008 11:12:39 -0700, tal_mcmahon <ta*********@hotmail.com>
wrote:
Struct are placed in memory on the stack,
No. This is a common, but serious misconception.

Structs are "value types". That means that the storage for the type is
allocated where the variable itself is stored. But only local variables
wind up on the stack. A struct used in a class, or in an array, or
anything else that is itself allocated on the heap will wind up on the
heap itself.
therefore faster and do not
need to be cleaned up with garbage collection.
Structs need to be collected when boxed. They also wind up collected
implicitly when they are part of a class that is collected.

In fact, if a struct winds up boxed on a regular basis that will suggest
that the data structure should have been a class after all.

Pete
Jul 28 '08 #8

"Author" <gn********@gmail.comwrote in message
news:00**********************************@f63g2000 hsf.googlegroups.com...
On Jul 28, 12:23 pm, "MC" <for.address.l...@www.ai.uga.edu.slash.mc>
wrote:
I know the basic differences between a struct and a class, but
apparently not good enough to know why some types/concepts are
implemented as struct whereas most types are implemented as class.
For example, why is DateTime implemented as a struct? Why is BitArray
implemented as a class, but BitVector32 a struct?
In general, in OO design, how do we determine if we should use struct
or class?

I think that, in general, structs are for small things that use a fixed
amount of memory.
Hmm, sounds reasonable, at least with regard to BitArray (resizable)
and BitVector32 (not resizable). But is this the only possible reason
that some types are implemented as struct and others class?

I've read the other responses of my question, but they seem to be off-
topic. I would like to hear some guruish comment about my question.

*****************************
I'm only a beginner myself, but nobody has mentioned that classes are
inheritable, whereas structs are not. If you are looking at this from an OOP
perspective, this is a huge difference.
Jul 29 '08 #9
To be honest I don't even remember the problems I had, I just remember that
every time I have used them I have ended up switching to classes so now I
don't bother.
Pete

Jul 29 '08 #10
On Jul 28, 7:33*pm, Author <gnewsgr...@gmail.comwrote:
In general, in OO design, how do we determine if we should use struct
or class?
A few distinguishing traits:

1) It is data-centric, not behavior-centric - e.g. Point is just a
tuple of two ints, Person is an entity with encapsulated operations.
Most methods on structs are just helpers to manipulate data, and could
usually be refactored as external methods. Most methods on classes
should encapsulate business logic related to entities those classes
model.

2) It has no identity - two structs are equal if the data within is
the same; for two structs with the same data, there is no way to
distinguish them, and using one in place of the other should give
identical results for any operation. C# enforces this by requiring
structs to define at least one field (since two identityless objects
which don't have data cannot be distinguished at all, so the result is
effectively a singleton). For example, if you get a Point with X=2 and
Y=3 from the first method, it is guaranteed that whether you pass that
same Point on to the second method, or create your own new Point with
the same values of X and Y and then pass that to the second method,
the actions performed by the second method will be exactly the same.
On the other hand, if you have two different Person instances, they
typically refer to two different people, even if their names (and
other data) match, so passing one instead of another to a business
method will do a different thing. In C#, this manifests itself in the
fact that classes have operator== which compares references (i.e. does
an identity comparison), and default implementation of Equals() for
classes also does reference comparison, while for structs Equals()
compares values of all fields, and there is no way to do a reference
comparison.

3) It is atomic with respect to updates - you cannot take an existing
Point and change just the X coordinate - the result would always be a
new Point. In C# terms, this means that structs are immutable (this
isn't enforced by the compiler, but is strongly recommended to follow
the practice nonetheless).

Simply put, the design difference between a struct and a class is the
difference between raw data (tuple, record, etc) and an entity. The
line can be blurry sometimes, but usually it is fairly obvious for
every specific case.

Of course, in real world, decision on using class vs struct is often
made for performance reasons - a good example is List<T>.Enumerator,
which is a struct, though it doesn't really fit the definition above.
Jul 29 '08 #11

"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
On Mon, 28 Jul 2008 11:53:40 -0700, Fredo <fr***@hotmail.comwrote:
>"Peter Morris" <mr*********@SPAMgmail.comwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
>>Nearly every time I have designed something as a struct I have later
discovered I need to be able to change the values in it and ended up
converting it to a class, so now I just don't bother with them any more.

You can change values in a struct as easily as you can in a class.

Well, yes and no.

There are some serious problems with making a struct mutable. The fact
that structs are _copied_ rather than references leads to code that either
doesn't work as expected (you think you've changed an instance but have
only changed a copy of an instance), or just doesn't compile, or is more
inconvenient than it could or should be.

Pete
Pete,

You're absolutely correct. I guess part of it is mentality.

I generally reserve structs for things that I think of as "non-trivial
datatypes". For example, point, size, rect, vector (mathematical, not in the
array sense), and so forth, are little more than data types that are
slightly more complex than the basic data types. So the fact that they pass
by value instead of by reference, just like the basic data types, makes
sense.

They can also be useful in the plain old C sense of struct (a collection of
related variables), though I generally go with a class in that case.

Pete
Jul 29 '08 #12
On Tue, 29 Jul 2008 07:31:58 -0700, Pavel Minaev <in****@gmail.comwrote:
On Jul 28, 7:33Â*pm, Author <gnewsgr...@gmail.comwrote:
>In general, in OO design, how do we determine if we should use struct
or class?

A few distinguishing traits:

1) It is data-centric, not behavior-centric [...]

2) It has no identity - two structs are equal if the data within is
the same [...]

3) It is atomic with respect to updates [...]
Of course, all of the above three apply to the String type, which is _not_
a struct.

All due respect, I think you missed one of the most important aspects of a
struct: it's usually something small, so that copying it around isn't
costly. And I think that there are a number of examples of classes that
would fit your three criteria but still are better as classes than structs.

Pete
Jul 30 '08 #13

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

Similar topics

1
by: Guilherme Pinto | last post by:
Hello. I am reading the book written by Bjarne Stroustrup called " The C++ Programming Language - Special Edition" and had a doubt which a think is really important to distinguish between the...
193
by: Michael B. | last post by:
I was just thinking about this, specifically wondering if there's any features that the C specification currently lacks, and which may be included in some future standardization. Of course, I...
11
by: theshowmecanuck | last post by:
As a matter of academic interest only, is there a way to programmatically list the 'c' data types? I am not looking for detail, just if it is possible, and what function could be used to...
48
by: yezi | last post by:
Hi, all: I want to record some memory pointer returned from malloc, is possible the code like below? int memo_index; int i,j; char *tmp; for (i=0;i<10;i++){
3
by: Bear | last post by:
Hello there How come it's possible to add values of the type "int" into an ArrayList when the Add member function only accepts values of type "object"? Is an int just a "typedef" of the Int32...
1
by: albert_reade | last post by:
Hello I was wondering if someone could please help me understand what I need to do in order to get this project to work. I just need some hints or a push in the right direction to get this to work,...
19
by: Marco Trapanese | last post by:
Hello, I need some help to understand what I'm doing :) I'm writing code for an embedded system and I need to create a navigable menu system. I'm going to use these structures: typedef...
2
by: Stefan Hoffmann | last post by:
hi @all, has anybody an explanation, why string? is not allowed for an auto-implemented property? namespace Test { class Class1 { int? NullableInt { get; set; }
50
by: Juha Nieminen | last post by:
I asked a long time ago in this group how to make a smart pointer which works with incomplete types. I got this answer (only relevant parts included): ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.