struct vs class in c#
I am little bit confused between when to use **struct** and when to use **class**. I know basically struct is ***value type*** and class is ***ref type***. So whenever you pass the value type to function it copies whole values to that local function memory and whenever pass objects of class to any function it just passes the reference of that object where all the value resides in memory. So I am looking for good example of where to use struct and class in terms of **performance optimization** and also **best programming practices**. Thanks for reviewing question.
Tagged:
5
Best Answer
-
Structs should be small and immutable with only value type members that represent a value that can only be represented when all of its child members are defined. **Programming Practices** In web development, it is pretty safe to say to always use a class. They are easier to manage and manipulate. In non-web development structs are used when you want a single value that has multiple parts. The best example are Vectors. In graphics programming generally all Vectors are structs, e.g. a Vector3 would have an X, Y and Z float values. Those should values never change, any manipulation of the underlying value should always create a new struct instead. Another use for structs in when you need to tightly pack byte values for serialization. [
http://www.developerfusion.com/article/84519/mastering-structs-in-c/][1] There are some rules out there that say structs should be no larger than 16bytes, however Microsoft ignores this rule as do other major frameworks such as Unity3d. What really matters is that the struct is a representation of a single value. **Performance Optimization** Generally classes will be faster because they do not need to be re-created every time they are passed into a method. The exceptions I have seen to this rule are when you have a very large collection of structs and need to perform operations on them. For example if I had one hundred thousand Vector3s in an array, passing the Vector3 to a method by ref type with an out parameter will perform better than using classes. The other exception goes pack to the byte packing and serialization to the GPU; byte serialization of structs should be much faster than classes because they can define the byte packing and should not need to serialize child value types. **Additional Links** * [
http://msdn.microsoft.com/en-us/library/aa664471%28v=vs.71%29.aspx][2] * [
http://msdn.microsoft.com/en-us/library/ms229017.aspx][3] [1]:
http://www.developerfusion.com/article/84519/mastering-structs-in-c/ [2]:
http://msdn.microsoft.com/en-us/library/aa664471%28v=vs.71%29.aspx [3]:
http://msdn.microsoft.com/en-us/library/ms229017.aspx8
Answers
-
Good discussion of struct storage:
http://blogs.msdn.com/b/ericlippert/archive/2010/09/30/the-truth-about-value-types.aspx0
Categories
- All Categories
- 6 AHS
- 39 Alpha
- 161 App Studio
- 4 Block Chain
- 4 Bot Platform
- 16 Connected Risk APIs
- 47 Data Fusion
- 30 Data Model Discovery
- 608 Datastream
- 1.3K DSS
- 577 Eikon COM
- 4.9K Eikon Data APIs
- 7 Electronic Trading
- Generic FIX
- 7 Local Bank Node API
- Trading API
- 2.7K Elektron
- 1.3K EMA
- 236 ETA
- 519 WebSocket API
- 33 FX Venues
- 10 FX Market Data
- 1 FX Post Trade
- 1 FX Trading - Matching
- 12 FX Trading – RFQ Maker
- 5 Intelligent Tagging
- 2 Legal One
- 20 Messenger Bot
- 2 Messenger Side by Side
- 9 ONESOURCE
- 7 Indirect Tax
- 59 Open Calais
- 264 Open PermID
- 39 Entity Search
- 2 Org ID
- PAM
- PAM - Logging
- 8.4K Private Comments
- 6 Product Insight
- Project Tracking
- ProView
- ProView Internal
- 20 RDMS
- 1.4K Refinitiv Data Platform
- 367 Refinitiv Data Platform Libraries
- 3 Refinitiv Due Diligence
- LSEG Due Diligence Portal API
- 3 Refinitiv Due Dilligence Centre
- Rose's Space
- 1.1K Screening
- 18 Qual-ID API
- 13 Screening Deployed
- 23 Screening Online
- 10 World-Check Customer Risk Screener
- 990 World-Check One
- 44 World-Check One Zero Footprint
- 45 Side by Side Integration API
- Test Space
- 3 Thomson One Smart
- 1.2K TR Internal
- Global Hackathon 2015
- 2 Specialists Who Code
- 10 TR Knowledge Graph
- 150 Transactions
- 142 REDI API
- 1.7K TREP APIs
- 4 CAT
- 21 DACS Station
- 117 Open DACS
- 1.1K RFA
- 103 UPA
- 172 TREP Infrastructure
- 224 TRKD
- 886 TRTH
- 5 Velocity Analytics
- 5 Wealth Management Web Services
- 59 Workspace SDK
- 9 Element Framework
- 5 Grid
- 13 World-Check Data File
- Yield Book Analytics
- 46 中文论坛