?
Solved

When to Use Struct instead of class

Posted on 2007-11-19
7
Medium Priority
?
1,114 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
Python: Series & Data Frames With Pandas

Learn the basics of Python’s pandas library of series & data frames and how we can use these tools for data manipulation.

 
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

Is Your Team Achieving Their Full Potential?

74% of employees feel they are not achieving their full potential. With Linux Academy, not only will you strengthen your team's core competencies but also their knowledge of of the newest IT topics.

With new material every week, we'll make sure that you stay ahead of the game.

Question has a verified solution.

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

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
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 tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
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…
Suggested Courses

800 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