473,769 Members | 1,959 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1590
wesley <we*@inthemix.c om.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.co m>
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.c om.au> schreef in bericht
news:OF******** ******@TK2MSFTN GP09.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******@skyne tWORK.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.co m>
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.c om.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.co m>
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.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
wesley <we*@inthemix.c om.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.co m>
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.c om.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.co m>
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
4178
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
17652
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 two questions: 1. Is it generally safe to "overlay" the structure on the buffer, e.g.: unsigned char buffer;
5
3305
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 to be in that single file, that worked OK. But when I divdie that file into several ones, I have many "invalid use of undefined type" errors. The four files are main.c, main.h, readLP.h, and readLP.c. In readLP.h
19
2639
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; } RtWidget;
16
3844
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
280
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
4356
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 that there is no difference between giving this function a
7
2262
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
5069
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 deleted, however it must be initialized by a function during run-time to contain so many users which each contain so many directories of which each contain so many files. I've completed the program and have it running flawlessly without implementing...
0
9579
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9420
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10205
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10035
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9984
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6662
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5441
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3556
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2811
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.