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

Structures vs. Classes

At work, our development team has a development standards document that

insists Structures should never be used. I'm looking to change this
standard but need a suitable argument in order to make the change. I
know that Structures are value types, sit on the stack, and are
generally more efficient to manipulate than reference types (i.e.
Classes). Structures cannot use inheritance, the finalize method or
default constructors. Can anyone think of a situation where using
Structures is more suitable than using Classes?

Mar 10 '06 #1
2 2376
"Never" is a rather extreme approach, but I can see how under some
circumstances, a company might make such a policy. Structures are value
types, and don't behave in quite the same way as classes, which are
reference types. They are not by any means the same as unmanaged structures.
In fact, abuse of structures can actually lead to *increased* memory usage,
since classes are stored in the heap, while structures are stored on the
stack. Garbage Collection manages the heap, but not the stack.

Structures can be useful when working with aggregates of primitive types.
They should definitely be used sparingly. And they should be used when one
needs to ensure that one is working with a value type. And that's about it.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer

Presuming that God is "only an idea" -
Ideas exist.
Therefore, God exists.

<th**********@gmail.com> wrote in message
news:11**********************@j52g2000cwj.googlegr oups.com...
At work, our development team has a development standards document that

insists Structures should never be used. I'm looking to change this
standard but need a suitable argument in order to make the change. I
know that Structures are value types, sit on the stack, and are
generally more efficient to manipulate than reference types (i.e.
Classes). Structures cannot use inheritance, the finalize method or
default constructors. Can anyone think of a situation where using
Structures is more suitable than using Classes?

Mar 10 '06 #2
> At work, our development team has a development standards document that
insists Structures should never be used. I'm looking to change this
standard but need a suitable argument in order to make the change. I
know that Structures are value types, sit on the stack, and are
generally more efficient to manipulate than reference types (i.e.
Classes). Structures cannot use inheritance, the finalize method or
default constructors. Can anyone think of a situation where using
Structures is more suitable than using Classes?


1. MS has about 100 structures in .net. What is good enough for MS is good
enough for your team. If the team truly has an aversion to structures, then
they should avoid using them as well as defining them. That means don't use
point, size, rectangle, color, etc. It also means dont use DictionaryEntry,
so no hashtables either. And so on. Now I believe that your team's stance
"Structures should never be used" means they should not be created, but that
you can use the MS provided structures. Nevertheless, you should use this
thought to make the team see that structures are not absolutely evil, and
thus an absolute prohibition is unwarranted.

2. I have a structure consisting of 4 bytes and an integer, and I've
arranged for it to occupy 8 bytes of storage. No can do with a class, and if
I have enough of them, memory usage will be impacted. Maybe you will need to
interop with pre-.net functionality. With structures, you can generally
mimic old style C structs. In other words, a structure is ideal for a data
layout that you have to work with (ie a data layout that was thrust upon you
rather than one you design yourself).

3. My general usage guide for a structure is (1) it aggregates a small
number of value types and strings, and (2) a structure variable's scope is
that of a local variable in a procedure. In other words, they are simple and
short lived. They behave like an integer, but maybe you need two integers.
This is what value types are all about, and your post indicates that you
understand. To not use them is like not using integers. This argument is
really a philosophical stance against the position of your team. If the team
won't embrace structures, maybe you could arrange for a team committee to
approve structures. My guess is that a very small number of structures would
pass the committee, and that is probably all you need.

Mar 10 '06 #3

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

Similar topics

9
by: OKB (not okblacke) | last post by:
For a variety of reasons, I'm interested in putting together some code that will allow me to created structures out of nested classes, something like: class class1: def methA(self): print...
11
by: Fred Bennett | last post by:
I have a simulation project in which data can naturally be held in structures for processing. There are calls to multiple functions involved. Execution speed is an issue. Do I take a big hit for...
1
by: kazack | last post by:
Hi all it's me again with another question as I got further in my book. The chapter I am in covers structres, abstract data and classes. I only read through to the end of the coverage on...
6
by: nick | last post by:
I have tried finding an answer to this, but most people just explain classes as a more modular way to program. It seems to me that (forgetting OO programming which I don't quite understand) the...
6
by: Ken Allen | last post by:
OK, I admit that I have been programming since before C++ was invented, and I have developed more than my share of assembly language systems, and even contributed to operating system and compiler...
7
by: OpticTygre | last post by:
Alright, so I'm messing around with some code, and I brought up a good question to myself. If creating a class called "Person", and filling that class with variables, properties like: Public...
14
by: pmclinn | last post by:
I've noticed that many programmers use classes to store data about such things like: Class Customers .....Phone ....ID ....Address End Class....
5
by: eBob.com | last post by:
I am trying to change some Structures to Classes. The Classes now look like this ... Public Class OneAttr Public AttrName As String Public Column As String Public Caption As String End Class...
4
by: zacks | last post by:
A common programming technique I use in VB is making a collection of structures. But if Option Strict is on (which I would prefer), the .Add that adds the structure to the collection is flagged...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.