473,320 Members | 1,988 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,320 software developers and data experts.

Struct VS Class

Hi there,

I am very curious on this code:

// declared as structure
[ StructLayout( LayoutKind.Sequential )]
public struct Overlapped
{
public IntPtr intrnal;
public IntPtr internalHigh;
public int offset;
public int offsetHigh;
public IntPtr hEvent;
}

// declared as class
[ StructLayout( LayoutKind.Sequential )]
public class Overlapped2
{
public IntPtr intrnal;
public IntPtr internalHigh;
public int offset;
public int offsetHigh;
public IntPtr hEvent;
}

This is the first time i saw people declaring in Class and later using into platform invocation.

So what is the performance of using class or structures.

As i need to do some low level stuff like smart card, based on my seniors, you need to past structures when you want to write some data into the card. Instead of using structures, can it be done with class too?

Well, i not sure how to explain, but something like this. What are the advantages and disadvantages of using class or structs?

Thanks.
--
Regards,
Chua Wen Ching :)
Nov 16 '05 #1
5 7171
Structs vs. Classes
Structs may seem similar to classes, but there are important differences that you should be aware of. First of all, classes are reference types and structs are value types. By using structs, you can create objects that behave like the built-in types and enjoy their benefits as well.

Heap or Stack?
When you call the New operator on a class, it will be allocated on the heap. However, when you instantiate a struct, it gets created on the stack. This will yield performance gains. Also, you will not be dealing with references to an instance of a struct as you would with classes. You will be working directly with the struct instance. Because of this, when passing a struct to a method, it's passed by value instead of as a reference.

Article from MSDN

Dincer Uyav
ne*********@welovedotnet.net

"Chua Wen Ching" wrote:
Hi there,

I am very curious on this code:

// declared as structure
[ StructLayout( LayoutKind.Sequential )]
public struct Overlapped
{
public IntPtr intrnal;
public IntPtr internalHigh;
public int offset;
public int offsetHigh;
public IntPtr hEvent;
}

// declared as class
[ StructLayout( LayoutKind.Sequential )]
public class Overlapped2
{
public IntPtr intrnal;
public IntPtr internalHigh;
public int offset;
public int offsetHigh;
public IntPtr hEvent;
}

This is the first time i saw people declaring in Class and later using into platform invocation.

So what is the performance of using class or structures.

As i need to do some low level stuff like smart card, based on my seniors, you need to past structures when you want to write some data into the card. Instead of using structures, can it be done with class too?

Well, i not sure how to explain, but something like this. What are the advantages and disadvantages of using class or structs?

Thanks.
--
Regards,
Chua Wen Ching :)

Nov 16 '05 #2
Hi Dincer Uyav,

Does it mean passing by value will increase more performance over by passing by reference?

Hmm, i thought pass by refernce should be faster right? Coz it only holds the address rather than the actual value.

Correct me if i am wrong. Thanks.
--
Regards,
Chua Wen Ching :)
"Dincer Uyav" wrote:
Structs vs. Classes
Structs may seem similar to classes, but there are important differences that you should be aware of. First of all, classes are reference types and structs are value types. By using structs, you can create objects that behave like the built-in types and enjoy their benefits as well.

Heap or Stack?
When you call the New operator on a class, it will be allocated on the heap. However, when you instantiate a struct, it gets created on the stack. This will yield performance gains. Also, you will not be dealing with references to an instance of a struct as you would with classes. You will be working directly with the struct instance. Because of this, when passing a struct to a method, it's passed by value instead of as a reference.

Article from MSDN

Dincer Uyav
ne*********@welovedotnet.net

"Chua Wen Ching" wrote:
Hi there,

I am very curious on this code:

// declared as structure
[ StructLayout( LayoutKind.Sequential )]
public struct Overlapped
{
public IntPtr intrnal;
public IntPtr internalHigh;
public int offset;
public int offsetHigh;
public IntPtr hEvent;
}

// declared as class
[ StructLayout( LayoutKind.Sequential )]
public class Overlapped2
{
public IntPtr intrnal;
public IntPtr internalHigh;
public int offset;
public int offsetHigh;
public IntPtr hEvent;
}

This is the first time i saw people declaring in Class and later using into platform invocation.

So what is the performance of using class or structures.

As i need to do some low level stuff like smart card, based on my seniors, you need to past structures when you want to write some data into the card. Instead of using structures, can it be done with class too?

Well, i not sure how to explain, but something like this. What are the advantages and disadvantages of using class or structs?

Thanks.
--
Regards,
Chua Wen Ching :)

Nov 16 '05 #3
When you pass by value you already know what the value is and have access to the value. With the pass by refrence you have one level of indirection and need to locate and access that memory address. Depending on the size of the value and refrence there is a point where a reference would be better than by value. Like a large struct and an array of them could degrade performance in this case you might just want to pass by refrence the start of the array of classes.

RAyRAy

"Chua Wen Ching" wrote:
Hi Dincer Uyav,

Does it mean passing by value will increase more performance over by passing by reference?

Hmm, i thought pass by refernce should be faster right? Coz it only holds the address rather than the actual value.

Correct me if i am wrong. Thanks.
--
Regards,
Chua Wen Ching :)
"Dincer Uyav" wrote:
Structs vs. Classes
Structs may seem similar to classes, but there are important differences that you should be aware of. First of all, classes are reference types and structs are value types. By using structs, you can create objects that behave like the built-in types and enjoy their benefits as well.

Heap or Stack?
When you call the New operator on a class, it will be allocated on the heap. However, when you instantiate a struct, it gets created on the stack. This will yield performance gains. Also, you will not be dealing with references to an instance of a struct as you would with classes. You will be working directly with the struct instance. Because of this, when passing a struct to a method, it's passed by value instead of as a reference.

Article from MSDN

Dincer Uyav
ne*********@welovedotnet.net

"Chua Wen Ching" wrote:
Hi there,

I am very curious on this code:

// declared as structure
[ StructLayout( LayoutKind.Sequential )]
public struct Overlapped
{
public IntPtr intrnal;
public IntPtr internalHigh;
public int offset;
public int offsetHigh;
public IntPtr hEvent;
}

// declared as class
[ StructLayout( LayoutKind.Sequential )]
public class Overlapped2
{
public IntPtr intrnal;
public IntPtr internalHigh;
public int offset;
public int offsetHigh;
public IntPtr hEvent;
}

This is the first time i saw people declaring in Class and later using into platform invocation.

So what is the performance of using class or structures.

As i need to do some low level stuff like smart card, based on my seniors, you need to past structures when you want to write some data into the card. Instead of using structures, can it be done with class too?

Well, i not sure how to explain, but something like this. What are the advantages and disadvantages of using class or structs?

Thanks.
--
Regards,
Chua Wen Ching :)

Nov 16 '05 #4
Dincer Uyav <Di********@discussions.microsoft.com> wrote:
Structs vs. Classes
Structs may seem similar to classes, but there are important
differences that you should be aware of. First of all, classes are
reference types and structs are value types. By using structs, you
can create objects that behave like the built-in types and enjoy
their benefits as well.

Heap or Stack?
When you call the New operator on a class, it will be allocated on
the heap. However, when you instantiate a struct, it gets created on
the stack. This will yield performance gains. Also, you will not be
dealing with references to an instance of a struct as you would with
classes. You will be working directly with the struct instance.
Because of this, when passing a struct to a method, it's passed by
value instead of as a reference.


Only local variables end up on the stack - if a value type is an
instance or static variable, it will still end up on the heap.

Reference type values are also passed by value - it's just that the
value passed is a reference. There's a big difference between this and
pass-by-reference.

See http://www.pobox.com/~skeet/csharp/parameters.html and
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
Nov 16 '05 #5
Thanks RAyRAy and Jon Skeet.

I will look into those urls. Thanks again.
--
Regards,
Chua Wen Ching :)
"Jon Skeet [C# MVP]" wrote:
Dincer Uyav <Di********@discussions.microsoft.com> wrote:
Structs vs. Classes
Structs may seem similar to classes, but there are important
differences that you should be aware of. First of all, classes are
reference types and structs are value types. By using structs, you
can create objects that behave like the built-in types and enjoy
their benefits as well.

Heap or Stack?
When you call the New operator on a class, it will be allocated on
the heap. However, when you instantiate a struct, it gets created on
the stack. This will yield performance gains. Also, you will not be
dealing with references to an instance of a struct as you would with
classes. You will be working directly with the struct instance.
Because of this, when passing a struct to a method, it's passed by
value instead of as a reference.


Only local variables end up on the stack - if a value type is an
instance or static variable, it will still end up on the heap.

Reference type values are also passed by value - it's just that the
value passed is a reference. There's a big difference between this and
pass-by-reference.

See http://www.pobox.com/~skeet/csharp/parameters.html and
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

Nov 16 '05 #6

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

Similar topics

21
by: Kilana | last post by:
I see this all the time in code: typedef struct a_struct { ... }differentName, *differentNamePtr; I understand how I can use it, but could someone tell me why the above is
2
by: SACHIN | last post by:
I have this class as part of a Consol application. using System; namespace Bugreport { /// <summary> /// This class tries to use the Class/Struct combination. /// </summary> class Class1 {
4
by: Steve | last post by:
I'll be the first to admit, I'm not entirely clear on the appropriate usage of either. From what I am reading in my books, a Struct and a Class are pretty much the same, with the difference being,...
15
by: Steven T. Hatton | last post by:
The following may strike many of you as just plain silly, but it represents the kind of delelima I find myself in when trying to make a design decision. This really is a toy project written for...
15
by: bugzilla | last post by:
hi,all, I have a C++ program need to convert to c language to be used in a emabedded system. the problem is that the original code was writtern in C++ language with Parent class and some child...
3
by: Karl M | last post by:
Hi everyone, I just notice some strange behaviors on the MS C++ compiler regarding struct default constructor, see the example bellow: struct MyStruct { int a; }; class MyClass { public:
4
by: DaHool | last post by:
Hi there !!! I browsed around the Internet in search for a solution of a little difficult problem i have in VB.NET.... However, i cannot find a suitable anwser anywhere, so i thought i'll give...
5
by: jwright | last post by:
I have decided to use a struct to collect my data. The input file is comma dilineated between almost all of the fields. Here is the code I have so far and a sample input and output file. ...
1
by: stromhau | last post by:
Hi, I have made a few classes in c++. They somehow cooperate doing some 3d stuff. Basically it is a moving camera acting as a flight, i have placed a lot of objects around the scene together with...
2
by: Ninereeds | last post by:
I'm messing around with using mixin-layers (look for papers by Yannis Smaragdakis and Don Batory) to define data structures. One issue is that nodes tend to have pointers to other nodes - the...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.