Struct VS Class

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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.
 
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
(e-mail address removed)
 
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 said:
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
(e-mail address removed)

Chua Wen Ching said:
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.
 
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 said:
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 said:
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
(e-mail address removed)

Chua Wen Ching said:
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.
 
Dincer Uyav said:
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
 
Back
Top