When to use Record vs Class vs Struct in C#?
When to use Record vs Class vs Struct in C#?
Here is a concise answer to the interview question "When to use Record vs Class vs Struct in C#?":
Use a struct
when you need a lightweight value type that is small, immutable, and has no complex behavior. Structs are stored on the stack and are passed by value, so they are ideal for small data structures that represent a single value, like points, colors, or key-value pairs[1][3].
Use a class
when you need a reference type that supports inheritance, polymorphism, and complex behavior. Classes are more flexible than structs and can represent more complex entities, but they are stored on the heap and passed by reference, so they have more overhead[1][3].
Use a record
when you need an immutable reference type that provides built-in value-based equality and concise syntax for creating data-centric types. Records are similar to cla...
middle
Gợi ý câu hỏi phỏng vấn
Chưa có bình luận nào