When a CLASS and STRUCT is used? [Resolved]

Posted by Akiii under C# on 7/29/2011 | Points: 10 | Views : 1235 | Status : [Member] | Replies : 7
Hi,
Can anyone explain, when should we use CLASS and STRUCT with examples ?

Thanks and Regards
Akiii




Responses

Posted by: RaviRanjanKr on: 7/31/2011 [Member] Starter | Points: 50

Up
0
Down

Resolved
Unlike class struct can be instantiated without using a new operator.
struct can declare constructor but they must take parameter. and there are many more specific feature of class and struct which will you know from there-[http://www.dotnetperls.com/struct-versus-class ].
take a look and you will know where need for what(class or Struct)

Thanks
Ravi Ranjan Kumar
http://raviranjankr.wordpress.com

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Hemanthlaxmi on: 7/29/2011 [Member] Starter | Points: 25

Up
0
Down
Structs can be useful in serial communications and COM interaction.

I don't feel you need to stick to those specific guidelines, it also depends on how you want to manage your memory.



If this helps you .
Please "Mark as Answer"

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Akiii on: 7/29/2011 [Member] Bronze | Points: 25

Up
0
Down
hi Hemant...
Thanks for your reply...
That means you want to say that structs and class are chosen with respect to memory management ?
What benefit one has by declaring either of them in real life scenarios ?

Regards
Akiii

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: A4u_6178 on: 7/29/2011 [Member] Starter | Points: 25

Up
0
Down
hi akiii,

structs are value types and benifit of value types is that they are slightly more efficient in terms of speed. Value types are store in the stack, while reference types are store in the heap. So, value types access the values directly from the stack. On the other hand reference types(class) are store on the heap and needs a pointer to point to the memory area in the heap where the object is stored. There is a slight penalty from going from the pointer to the memory area and dereferencing it.


If you copy a struct, C# creates a new copy of the object and assigns the copy of the object to a separate struct instance. However, if you copy a class, C# creates a new copy of the reference to the object and assigns the copy of the reference to the separate class instance.

Thanks & Regards,

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Akiii on: 7/29/2011 [Member] Bronze | Points: 25

Up
0
Down
Hi A4u_6178......
Your point is clearly understood......thanks for explaining..
Presently i am working in a web application project......all my DAL logic and business layer logic resides in Classes . Till now i and the whole team of mine haven't yet used Struct . So can you tell me now, just for the efficient memory management concern should we use Struct ?

Thanks and Regards
Akiii

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Akiii on: 8/1/2011 [Member] Bronze | Points: 25

Up
0
Down
Excellent article Ravi....
I mark that as an answer..

Thanks and Regards
Akiii

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: RaviRanjanKr on: 8/1/2011 [Member] Starter | Points: 25

Up
0
Down
I am Glad you like reference Link.
Thanks for marking it as an answer.

Thanks
Ravi Ranjan Kumar
http://raviranjankr.wordpress.com

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response