What is the difference between a struct and a class in C#?
Ask by Ravi Vishwakarma
Updated 06 Jun 2023
Structs and classes are both user-defined data types in C#. However, there are some key differences between the two.
aandb, and you assign the value ofatob,bwill contain the same value asa, not a reference toa.In general, structs should be used for data types that are small and simple, such as Point or Color. Classes should be used for data types that are complex or that need to be inherited from, such as Person or Customer.
Here is a table that summarizes the key differences between structs and classes in C#:
Struct
Class