472,805 Members | 1,568 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Structures and Classes

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 systems over the
years. I have developed code in more than 30 distinct programming
languages for a wide cariety of industries. But this issue of structures
in C# is beginning to annoy me.

I should also make it clear that I am not a big supporter of the C++
convention that the only difference between a class and a structure is
whether or not the data members, private or otherwise, can be treated as
a single entity. I have made strong cases in C++ code for the structure
to be a data member of a class, not make the member functions part of
the structure.

There are times when a structure is extemely useful, however, such as
when communicating with an exising applicaiton or file that stores its
data in a specific layout that is well defined using a C++ structure.

I have been using C# for some time now, and I must admit that I find the
language a significant improvement over C++ (aside from speed at times).
I especially like how enumerations are a part of the language - I
believe the C# approach to be far better than the C/C++ approach (not
claiming this approach is unique to C#). But this issue of structures
vs. classes keeps raising its head.

I sometimes write code, in the form of services, that must communicate
with custom device drivers, often passing the control structures in
predefined record layouts. In C++ these are simple structures, and I
have been using the same in C# most of the time -- except where the
structures are not simple, and it became much easier to define a class
with MarshalIn() and MarshalOut() methods that transfer the data members
sequentially. This is especially true when some of the structures nest
other structures several levels deep.

The one thing that bothers me the most is this concept that a structure
is a 'value' type and a class is an object type. This means that there
are certain times when passing structures around in code rquires
'boxing' or other special steps, or it cannot be done cleanly.

What happens in C# when I define a class that has a structure as its
data member (perhaps its only data member)? The structure is still
allocated on the stack, as opposed to from the heap, and so there would
still appear to be some problems with passing things around.

I am coming to the conclusion that the usefullness of structures in C#
is very limited and effective only in isolated cases. It would seem that
the main C# code should always use classes with atomic data members, and
either create and use the structure only as part of I/O operations (some
overhead in the creation and population of the structure), or use custom
Marshal methods to deal with each atomic field on its own. The style of
defining a structure to be used as a single I/O or parameter object and
then adding methods to the structure definition so it also appears to be
a class is not something good or effective in C# (although some C++ code
I have encountered relies heavily on this approach).

Am I wrong here? Have I missed something?

-ken
Nov 17 '05 #1
6 4414
Ken,

At times I agree with you on this. It depends on the situation though.
For example, I see Point and Size as being really basic types which don't
need class semantics, and should be treated as value types. However, I
don't see many other uses (except in the case where you want to implement a
rollback mechanism of some kind, and implement IDisposable on the structure,
and use that to perform rollback operations in a using statement, and don't
want to allocate an object on the heap to do this).

There is one part of your post that is incorrect. You state:

What happens in C# when I define a class that has a structure as its
data member (perhaps its only data member)? The structure is still
allocated on the stack, as opposed to from the heap, and so there would
still appear to be some problems with passing things around.

When you have a field in a class that is a value type, that value type
is not copied on the stack. The memory for that value type is in the memory
taken up by the class instance itself, and moves where the instance moves.
It's the same as having a field of type int or bool, it's just part of the
type.

Basically, what it comes down to is you use structures in your
programming where you feel copy-on-assignment semantics are appropriate.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Ken Allen" <ke******@sympatico.ca> wrote in message
news:uu*************@TK2MSFTNGP15.phx.gbl...
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 systems over the years.
I have developed code in more than 30 distinct programming languages for a
wide cariety of industries. But this issue of structures in C# is
beginning to annoy me.

I should also make it clear that I am not a big supporter of the C++
convention that the only difference between a class and a structure is
whether or not the data members, private or otherwise, can be treated as a
single entity. I have made strong cases in C++ code for the structure to
be a data member of a class, not make the member functions part of the
structure.

There are times when a structure is extemely useful, however, such as when
communicating with an exising applicaiton or file that stores its data in
a specific layout that is well defined using a C++ structure.

I have been using C# for some time now, and I must admit that I find the
language a significant improvement over C++ (aside from speed at times). I
especially like how enumerations are a part of the language - I believe
the C# approach to be far better than the C/C++ approach (not claiming
this approach is unique to C#). But this issue of structures vs. classes
keeps raising its head.

I sometimes write code, in the form of services, that must communicate
with custom device drivers, often passing the control structures in
predefined record layouts. In C++ these are simple structures, and I have
been using the same in C# most of the time -- except where the structures
are not simple, and it became much easier to define a class with
MarshalIn() and MarshalOut() methods that transfer the data members
sequentially. This is especially true when some of the structures nest
other structures several levels deep.

The one thing that bothers me the most is this concept that a structure is
a 'value' type and a class is an object type. This means that there are
certain times when passing structures around in code rquires 'boxing' or
other special steps, or it cannot be done cleanly.

What happens in C# when I define a class that has a structure as its data
member (perhaps its only data member)? The structure is still allocated on
the stack, as opposed to from the heap, and so there would still appear to
be some problems with passing things around.

I am coming to the conclusion that the usefullness of structures in C# is
very limited and effective only in isolated cases. It would seem that the
main C# code should always use classes with atomic data members, and
either create and use the structure only as part of I/O operations (some
overhead in the creation and population of the structure), or use custom
Marshal methods to deal with each atomic field on its own. The style of
defining a structure to be used as a single I/O or parameter object and
then adding methods to the structure definition so it also appears to be a
class is not something good or effective in C# (although some C++ code I
have encountered relies heavily on this approach).

Am I wrong here? Have I missed something?

-ken

Nov 17 '05 #2
Hello Ken,

You can find this discussion of some interest (mind the line wraps):

http://groups-beta.google.com/group/...cd85ecd5de4862

--
Sincerely,
Dmytro Lapshyn [Visual Developer - Visual C# MVP]
"Ken Allen" <ke******@sympatico.ca> wrote in message
news:uu*************@TK2MSFTNGP15.phx.gbl...
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 systems over the years.
I have developed code in more than 30 distinct programming languages for a
wide cariety of industries. But this issue of structures in C# is
beginning to annoy me.

I should also make it clear that I am not a big supporter of the C++
convention that the only difference between a class and a structure is
whether or not the data members, private or otherwise, can be treated as a
single entity. I have made strong cases in C++ code for the structure to
be a data member of a class, not make the member functions part of the
structure.

There are times when a structure is extemely useful, however, such as when
communicating with an exising applicaiton or file that stores its data in
a specific layout that is well defined using a C++ structure.

I have been using C# for some time now, and I must admit that I find the
language a significant improvement over C++ (aside from speed at times). I
especially like how enumerations are a part of the language - I believe
the C# approach to be far better than the C/C++ approach (not claiming
this approach is unique to C#). But this issue of structures vs. classes
keeps raising its head.

I sometimes write code, in the form of services, that must communicate
with custom device drivers, often passing the control structures in
predefined record layouts. In C++ these are simple structures, and I have
been using the same in C# most of the time -- except where the structures
are not simple, and it became much easier to define a class with
MarshalIn() and MarshalOut() methods that transfer the data members
sequentially. This is especially true when some of the structures nest
other structures several levels deep.

The one thing that bothers me the most is this concept that a structure is
a 'value' type and a class is an object type. This means that there are
certain times when passing structures around in code rquires 'boxing' or
other special steps, or it cannot be done cleanly.

What happens in C# when I define a class that has a structure as its data
member (perhaps its only data member)? The structure is still allocated on
the stack, as opposed to from the heap, and so there would still appear to
be some problems with passing things around.

I am coming to the conclusion that the usefullness of structures in C# is
very limited and effective only in isolated cases. It would seem that the
main C# code should always use classes with atomic data members, and
either create and use the structure only as part of I/O operations (some
overhead in the creation and population of the structure), or use custom
Marshal methods to deal with each atomic field on its own. The style of
defining a structure to be used as a single I/O or parameter object and
then adding methods to the structure definition so it also appears to be a
class is not something good or effective in C# (although some C++ code I
have encountered relies heavily on this approach).

Am I wrong here? Have I missed something?

-ken


Nov 17 '05 #3
Nicholas Paldino [.NET/C# MVP] wrote:
Ken,

At times I agree with you on this. It depends on the situation though. <snip/> Basically, what it comes down to is you use structures in your
programming where you feel copy-on-assignment semantics are appropriate.

Hope this helps.


Yes, this is the basic conclustion that I was coming to (thank you for
the correction on the structure as a class data member; I had meant to
pose that more as a question).

I do see value of wrapping the structure inside a class when the
structure contents are to be used as part of communication with another
component, permitting the buffer to be read and written as a unit
(although marshalling may still be involved).

My problems lie in the fact that 90% of the projects I consider deal
with communication with existing (unmanaged) components, and when the
component is in the kernel, I am forced to use unmanaged Win32 API calls
(DeviceIoControl), which means marshalling is involved in one manner or
another.

This is where I am forced to selected between marshalling a structure or
marshalling the individual data members themselves. Writing interop code
is not somthing Visual Studio makes easier!

-ken
Nov 17 '05 #4
Ken Allen <ke******@sympatico.ca> wrote:

<snip>
What happens in C# when I define a class that has a structure as its
data member (perhaps its only data member)? The structure is still
allocated on the stack, as opposed to from the heap, and so there would
still appear to be some problems with passing things around.
No, that's not true. The idea that value types always end up on the
stack is a mistake due to people being lazy when trying to explain what
goes where.

See http://www.pobox.com/~skeet/csharp/memory.html
I am coming to the conclusion that the usefullness of structures in C#
is very limited and effective only in isolated cases.
Pretty limited, yes - basically where you really need a value type,
which is pretty rarely given that many useful ones are already in the
framework.

I don't see why that's a problem though - it's just not the same as how
C/C++ viewed structures.
It would seem that
the main C# code should always use classes with atomic data members, and
either create and use the structure only as part of I/O operations (some
overhead in the creation and population of the structure), or use custom
Marshal methods to deal with each atomic field on its own. The style of
defining a structure to be used as a single I/O or parameter object and
then adding methods to the structure definition so it also appears to be
a class is not something good or effective in C# (although some C++ code
I have encountered relies heavily on this approach).


In what way does adding methods to the structure make it "appear to be
a class"? You need to forget the concept that structures are for data
only - the difference between structures and classes is just that
structures are value types and classes are reference types.

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

You know you can marshal classes, right? You don't ^have^ to use
structures. You can apply the marshaling attributes to a class and it
should work. There are some slight differences, but for the most part, it
works.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Ken Allen" <ke******@sympatico.ca> wrote in message
news:uO**************@TK2MSFTNGP14.phx.gbl...
Nicholas Paldino [.NET/C# MVP] wrote:
Ken,

At times I agree with you on this. It depends on the situation
though.

<snip/>
Basically, what it comes down to is you use structures in your
programming where you feel copy-on-assignment semantics are appropriate.

Hope this helps.


Yes, this is the basic conclustion that I was coming to (thank you for the
correction on the structure as a class data member; I had meant to pose
that more as a question).

I do see value of wrapping the structure inside a class when the structure
contents are to be used as part of communication with another component,
permitting the buffer to be read and written as a unit (although
marshalling may still be involved).

My problems lie in the fact that 90% of the projects I consider deal with
communication with existing (unmanaged) components, and when the component
is in the kernel, I am forced to use unmanaged Win32 API calls
(DeviceIoControl), which means marshalling is involved in one manner or
another.

This is where I am forced to selected between marshalling a structure or
marshalling the individual data members themselves. Writing interop code
is not somthing Visual Studio makes easier!

-ken

Nov 17 '05 #6
Nicholas Paldino [.NET/C# MVP] wrote:
Ken,

You know you can marshal classes, right? You don't ^have^ to use
structures. You can apply the marshaling attributes to a class and it
should work. There are some slight differences, but for the most part, it
works.

Is one assured of the order in which data members in a class will be
marhalled? One of the benefits of using a structure marked as sequential
layout is that one is assured bf the byte ordering for the marshalling.
It is not intuitive that a class would marshal as cleanly, especially
when the data memebers are strings or other reference objects - it would
seem that a 'field by field' marshalling procedure would be more
effective (but more coding effort?).

-ken
Nov 17 '05 #7

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

Similar topics

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...
1
by: Bob Rock | last post by:
Hello, in the last few days I've made my first few attempts at creating mixed C++ managed-unmanaged assemblies and looking aftwerwards with ILDASM at what is visible in those assemblies from a...
6
by: moi | last post by:
hi, im fairly new to c++ and as part of a module at uni im learning it. i know the basics i suppose, but as our final hand-in we have to alter code we wrote for an earlier assignment to use...
14
by: Pratts | last post by:
I am a new one who have joined u plz try to help me bcoz i could not find ny sutiable answer foer this Question Qus>>why do we need classes when structures provide similar functionality??
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...
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....
2
by: Paul | last post by:
I have a situation that I want to work but don't know the proper way to handle it. I want to create a User Defined "Type" (structure, class or enumeration). I'm not sure what it should be or...
2
by: thomasfarrow | last post by:
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...
3
by: ArmsTom | last post by:
I was using structures to store information read from a file. That was working fine for me, but then I read that anything stored in a structure is added to the stack and not the heap. So, I made...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.