473,396 Members | 2,009 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,396 software developers and data experts.

The necessity of struct

Hi,

What's the reasoning of having struct in .Net? Since they hardly differ with
classes? Are there any advantages using struct instead of classes?

Thanks
Jul 21 '05 #1
8 1553
wesley <we*@inthemix.com.au> wrote:
What's the reasoning of having struct in .Net? Since they hardly differ with
classes? Are there any advantages using struct instead of classes?


I disagree with your idea that they "hardly differ" from classes - they
are value types instead of reference types, which makes a huge
difference in semantics.

As to why they're present at all - interop pretty much requires them,
for a start. (This is one reason Java can get away with just classes
and Java-defined primitives - it doesn't have the same kind of interop
capabilities as .NET.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #2
Structs are allocated on the stack while classes are allocated on the heap.
(De)allocations on the stack of objects is faster. However the objects
should be rather small (eg Person {firstname, lastname}) since stacksize is
limited.
Structs are value types while classes are reference types.
Structs have no destructors.

Yves

"wesley" <we*@inthemix.com.au> schreef in bericht
news:OF**************@TK2MSFTNGP09.phx.gbl...
Hi,

What's the reasoning of having struct in .Net? Since they hardly differ with classes? Are there any advantages using struct instead of classes?

Thanks

Jul 21 '05 #3
phoenix <pa******@skynetWORK.be> wrote:
Structs are allocated on the stack while classes are allocated on the heap.


That's not always the case.

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
Jul 21 '05 #4
> > What's the reasoning of having struct in .Net? Since they hardly differ
with
classes? Are there any advantages using struct instead of classes?


I disagree with your idea that they "hardly differ" from classes - they
are value types instead of reference types, which makes a huge
difference in semantics.

As to why they're present at all - interop pretty much requires them,
for a start. (This is one reason Java can get away with just classes
and Java-defined primitives - it doesn't have the same kind of interop
capabilities as .NET.)


You can use Java to call native dll functions. I'm not quiet sure how the
internal works, but I've written Java apps that do office automation using
Jawin. Is the Interop the main reason for struct? Or are there other
significant reasons?

Thanks,

wes

Jul 21 '05 #5
wesley <we*@inthemix.com.au> wrote:
As to why they're present at all - interop pretty much requires them,
for a start. (This is one reason Java can get away with just classes
and Java-defined primitives - it doesn't have the same kind of interop
capabilities as .NET.)
You can use Java to call native dll functions.


Yes, via JNI. However, that's significantly harder than .NET where you
can just directly call DLL functions - you don't need to write wrapper
libraries in C/C++.
I'm not quiet sure how the
internal works, but I've written Java apps that do office automation using
Jawin.
Yup - you have to use another product in order to make the interop
easier.
Is the Interop the main reason for struct? Or are there other
significant reasons?


Well, my own feeling is that interop is the main reason for structs,
but I couldn't say for sure that that was what the designers had in
mind.

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

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
wesley <we*@inthemix.com.au> wrote:
> As to why they're present at all - interop pretty much requires them,
> for a start. (This is one reason Java can get away with just classes
> and Java-defined primitives - it doesn't have the same kind of interop
> capabilities as .NET.)
You can use Java to call native dll functions.


Yes, via JNI. However, that's significantly harder than .NET where you
can just directly call DLL functions - you don't need to write wrapper
libraries in C/C++.
I'm not quiet sure how the
internal works, but I've written Java apps that do office automation
using
Jawin.


Yup - you have to use another product in order to make the interop
easier.
Is the Interop the main reason for struct? Or are there other
significant reasons?


Well, my own feeling is that interop is the main reason for structs,
but I couldn't say for sure that that was what the designers had in
mind.


Interop is a good reason, perhaps a primary reason, but I think that there
is enough value to justify structures even if interop was non-existant. Its
less common for some, I suppose, but it permits considerable flexibility
which you wouldnt have access to without structures, most notably C#
pointers(which you can treat like C\C++ pointers over a byte array, read in
the array and map the structure over it; this should get a big boost with
the fixed keyword in C# 2.0) and real value semantics for types that need it
rather than immutability and cloning, especially since there is no way to
create copy-on-write semantics other than by using structs or explicit clone
passing(last thing I want is a byclone calling convention).
Value semantics and stack\class inline allocation are rather valuable
things, IMHO. You could go the route that C++ seems to be going for whidbey
and find a way to allocate reference types on the stack, but I think that
would be adding needless complexity to the language(not sure how it works
all in all, I just don't think it fits the C# model. I'm also not sure how
they are passed to non C++ methods) without adding much of the value.


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

Jul 21 '05 #7
> >> > As to why they're present at all - interop pretty much requires them,
> for a start. (This is one reason Java can get away with just classes
> and Java-defined primitives - it doesn't have the same kind of interop > capabilities as .NET.)

You can use Java to call native dll functions.
Yes, via JNI. However, that's significantly harder than .NET where you
can just directly call DLL functions - you don't need to write wrapper
libraries in C/C++.
I'm not quiet sure how the
internal works, but I've written Java apps that do office automation
using
Jawin.


Yup - you have to use another product in order to make the interop
easier.
Is the Interop the main reason for struct? Or are there other
significant reasons?


Well, my own feeling is that interop is the main reason for structs,
but I couldn't say for sure that that was what the designers had in
mind.


Interop is a good reason, perhaps a primary reason, but I think that there
is enough value to justify structures even if interop was non-existant.

Its less common for some, I suppose, but it permits considerable flexibility
which you wouldnt have access to without structures, most notably C#
pointers(which you can treat like C\C++ pointers over a byte array, read in the array and map the structure over it; this should get a big boost with
the fixed keyword in C# 2.0) and real value semantics for types that need it rather than immutability and cloning, especially since there is no way to
create copy-on-write semantics other than by using structs or explicit clone passing(last thing I want is a byclone calling convention).
Value semantics and stack\class inline allocation are rather valuable
things, IMHO. You could go the route that C++ seems to be going for whidbey and find a way to allocate reference types on the stack, but I think that
would be adding needless complexity to the language(not sure how it works
all in all, I just don't think it fits the C# model. I'm also not sure how
they are passed to non C++ methods) without adding much of the value.


Strange that the syntax for using struct and classes are exactly the same.
At a glance people could have mistaken a struct for a class, until either
people read the documentation or the compiler informs you that you can't set
a struct to null. Perhaps if the syntax is different clearly identify which
one is which that will probably be better, IMO.

wes
Jul 21 '05 #8
wesley <we*@inthemix.com.au> wrote:
Strange that the syntax for using struct and classes are exactly the same.
Not really - it's called consistency.
At a glance people could have mistaken a struct for a class, until either
people read the documentation or the compiler informs you that you can't set
a struct to null. Perhaps if the syntax is different clearly identify which
one is which that will probably be better, IMO.


If you use any type without reading its documentation, you're bound to
run into trouble. The information is available at a glance in the docs,
even in the MSDN index (it will say Foo structure instead of Foo class)
if it's a framework structure. In the overview, a struct will always
derive from ValueType, whereas a reference type never will.

I can't remember ever mistaking a reference type for a value type or
vice versa.

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

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

Similar topics

37
by: WittyGuy | last post by:
Hi, I wonder the necessity of constructor and destructor in a Abstract Class? Is it really needed? ? Wg http://www.gotw.ca/resources/clcm.htm for info about ]
5
by: Roy Hills | last post by:
When I'm reading from or writing to a network socket, I want to use a struct to represent the structured data, but must use an unsigned char buffer for the call to sendto() or recvfrom(). I have...
5
by: PCHOME | last post by:
Hello! I am working on dividing a single C file into several files. Now I encounter a problem about the global variables and can not find a way to solve it. All global variables and codes used...
19
by: Russell Shaw | last post by:
Hi, I have two structs in a header file, and they reference each other, causing a compile error. Is there a standard way to deal with this? typedef struct { ... RtAction *actions; }...
16
by: burn | last post by:
Hello, i am writing a program under linux in c and compile my code with make and gcc. Now i have 4 files: init.c/h and packets.c/h. Each header-file contains some: init.h: struct xyz {
8
by: wesley | last post by:
Hi, What's the reasoning of having struct in .Net? Since they hardly differ with classes? Are there any advantages using struct instead of classes? Thanks
5
by: Johs32 | last post by:
I have a struct "my_struct" and a function that as argument takes a pointer to this struct: struct my_struct{ struct my_struct *new; }; void my_func(struct my_struct *new); I have read...
7
by: Alex | last post by:
If I have two struct. See below: struct s1 { int type; int (*destroy)(struct s1* p); } struct s2 { struct s1 base;
4
by: hobbes992 | last post by:
Howdy folks, I've been working on a c project, compiling using gcc, and I've reached a problem. The assignment requires creation of a two-level directory file system. No files have to be added or...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.