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.
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.
Đăng nhập để tham gia thảo luận cùng mọi người.
Chưa có bình luận nào
Hãy là người đầu tiên chia sẻ suy nghĩ của bạn.