K
Ken Allen
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
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