Connecting Tech Pros Worldwide Help | Site Map

Struct VS Class

Chua Wen Ching
Guest
 
Posts: n/a
#1: Nov 16 '05
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 :)
Dincer Uyav
Guest
 
Posts: n/a
#2: Nov 16 '05

re: Struct VS Class


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
newsaccount@welovedotnet.net

"Chua Wen Ching" wrote:
[color=blue]
> 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 :)[/color]
Chua Wen Ching
Guest
 
Posts: n/a
#3: Nov 16 '05

re: Struct VS Class


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:
[color=blue]
> 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
> newsaccount@welovedotnet.net
>
> "Chua Wen Ching" wrote:
>[color=green]
> > 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 :)[/color][/color]
RAyRAy
Guest
 
Posts: n/a
#4: Nov 16 '05

re: Struct VS Class


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:
[color=blue]
> 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:
>[color=green]
> > 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
> > newsaccount@welovedotnet.net
> >
> > "Chua Wen Ching" wrote:
> >[color=darkred]
> > > 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 :)[/color][/color][/color]
Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#5: Nov 16 '05

re: Struct VS Class


Dincer Uyav <DincerUyav@discussions.microsoft.com> wrote:[color=blue]
> 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.[/color]

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 - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Chua Wen Ching
Guest
 
Posts: n/a
#6: Nov 16 '05

re: Struct VS Class


Thanks RAyRAy and Jon Skeet.

I will look into those urls. Thanks again.
--
Regards,
Chua Wen Ching :)


"Jon Skeet [C# MVP]" wrote:
[color=blue]
> Dincer Uyav <DincerUyav@discussions.microsoft.com> wrote:[color=green]
> > 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.[/color]
>
> 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 - <skeet@pobox.com>
> http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too
>[/color]
Closed Thread


Similar C# / C Sharp bytes