Solved

When to Use Struct instead of class

Posted on 2007-11-19
7
1,097 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

911 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

24 Experts available now in Live!

Get 1:1 Help Now