Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

When to Use Struct instead of class

Posted on 2007-11-19
7
Medium Priority
?
1,122 Views
Last Modified: 2013-12-17
When to use struct instead of class in c#?
0
Comment
Question by:Rahamathulla_J
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 1000 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

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!

Question has a verified solution.

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

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…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

636 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