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 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
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
> > 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
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
"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
> >> > 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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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 ]
|
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...
|
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...
|
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;
}...
|
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
{
|
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
|
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...
|
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;
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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,...
|
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...
|
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,...
| |