Global Directory
Global Directory
EXPLORE OUR SITES
London Stock Exchange Group
LSEG Data & Analytics
MyAccount
LSEG Perspectives
London Stock Exchange
FTSE Russell
LCH
Contact Us
Home
TR Internal
struct vs class in c#
Jenishkumar Rabadiya
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.
Find more posts tagged with
c#
.net
object
oop
refinitiv-internal
Accepted answers
Timothy Sullivan
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(v=vs.71).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(v=vs.71).aspx
[3]:
http://msdn.microsoft.com/en-us/library/ms229017.aspx
All comments
Timothy Sullivan
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(v=vs.71).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(v=vs.71).aspx
[3]:
http://msdn.microsoft.com/en-us/library/ms229017.aspx
Igor Berger
Good discussion of struct storage:
http://blogs.msdn.com/b/ericlippert/archive/2010/09/30/the-truth-about-value-types.aspx
Quick Links
All Forums
Recent Questions
Terms of use
Privacy & Cookie Statement
Cookies settings
Do not sell my info
Whistleblowing
UK Bribery Act
Modern Slavery Act