Tell me the role of structs in C#. Why would you choose to define a type as a struct instead of a class?

Submitted by: Muhammad
Structs are used to create structures, a value type that is used to represent a record. Structs play a similar role to classes, which are a reference type in C#. Since structs are value types, they are allocated and deallocated on the stack or inline in containing types. This generally makes structs cheaper to allocate or deallocate than classes, which are reference types. Reference types are allocated on the heap and garbage-collected, which tends to take up more resources. If the instances of a type are small and short-lived, it makes sense to define a value type using structs.

The opposite is true when it comes to boxing and unboxing. A value type can get boxed when it is cast to a reference type or some other interface it implements. Because boxes are objects allocated to the heap and deallocated via garbage collection, too much boxing and unboxing of a value can negatively impact performance. Reference types like classes are preferred in these situations.
Submitted by: Muhammad

Read Online Unity Developer Job Interview Questions And Answers