Solved

When to Use Struct instead of class

Posted on 2007-11-19
7
1,101 Views
Last Modified: 2013-12-17
When to use struct instead of class in c#?
0
Comment
Question by:Rahamathulla_J
7 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 20317826
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20317903
0
 
LVL 29

Accepted Solution

by:
Göran Andersson earned 250 total points
ID: 20318041
In .NET, structures are used to create types with value type semantics. Value types are for example the built in structs like int, long, bool, single and double.

If you are in doubt, don't have any performance problems, or if you just don't know that much about structs, use a class.

A struct should be small (recommended max size is 16 bytes), immutable (doesn't change once it's created), and don't contain references (unless perhaps to strings, which are also immutable).
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Expert Comment

by:administradores
ID: 20318179
In the Visual Studio documentation there is a good explantion of the differences between using Classes or Structures to manage data.

I usually combine structures and classes.
0
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 20322450
I completely agree to GreenGhost  Indeed it's recommended to use structures with max size 16 bytes.
Say, for clarity, it's four integer type fields. Or three integers and couple ofbooleans,Etc. At least these recommendations are given in MS books and I liked GreenGhost answer most..
0
 
LVL 5

Expert Comment

by:administradores
ID: 20325728
This link was good, better that GreenGhost comment for me, sorry, http://www.jaggersoft.com/pubs/StructsVsClasses.htm

The 16byte is not a mystery and as i stated, the good place to undertsand structures, object, classes or whatever is the documentation, but peepz dosen't like ro read it.


If you wanted me to copy paste i'd did...

Classes are reference types and structures are value types. Reference types are allocated on the heap, and memory management is handled by the garbage collector. Value types are allocated on the stack or inline and are deallocated when they go out of scope. In general, value types are cheaper to allocate and deallocate. However, if they are used in scenarios that require a significant amount of boxing and unboxing, they perform poorly as compared to reference types. For more information, see Boxing and Unboxing (C# Programming Guide).

For additional information about value types and reference types, see the Common Type System Overview.

Consider defining a structure instead of a class if instances of the type are small and commonly short-lived or are commonly embedded in other objects.
Do not define a structure unless the type has all of the following characteristics:
It logically represents a single value, similar to primitive types (integer, double, and so on).

It has an instance size smaller than 16 bytes.

It is immutable.

It will not have to be boxed frequently.

If one or more of these conditions are not met, create a reference type instead of a structure. Failure to adhere to this guideline can negatively impact performance.

Portions Copyright 2005 Microsoft Corporation. All rights reserved.

Portions Copyright Addison-Wesley Corporation. All rights reserved.

For more information on design guidelines, see the "Framework Design Guidelines: Conventions, Idioms, and Patterns for Reusable .NET Libraries" book by Krzysztof Cwalina and Brad Abrams, published by Addison-Wesley, 2005.



0
 
LVL 29

Expert Comment

by:Göran Andersson
ID: 20325883
You don't need to copy, just link to the page:

http://msdn2.microsoft.com/en-us/library/ms229017.aspx
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question