Solved

When to Use Struct instead of class

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

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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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 29

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Convert string to date 3 40
Pass through dll 2 36
.Net application crashing 6 20
What .NET website keeps me current? 9 29
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

708 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now