Ynoa Pedro
Posted on November 3, 2021
Little drops on C# Interview Questions series
The intention behind this series is to answer with a little bit of context or examples of some of the most common C# interview questions.
Hey friends long time no see but continuing our series on class x structs
Structs
Struts are value-type variables stored on the stack as we talked about in Part 1 of this series and by consequence having faster retrieval, structs cannot be inherited.
Classes
Classes are as we say the blueprint of an object(which is a class instantiated) storing it on the heap, we can use a default constructor and we can use inheritance.
When to use each
Classes are an overall blueprint for objects to be used and manipulated throughout the application, like domain objects.
Structs are data structures best suited for small primary data that is not intended to be modified after being created, like coordinates or other fixed data that you might need.
Disclaimer
As @daviholandas said the structs can be allocated on the heap if its referenced by a reference type.
Wrapping up
struct and classes can have constructors, constants, fields, methods, properties, indexers, operators, events and nested types.
struct and classes can implement interfaces.
struct does not support destructor or default constructor(parameterless).
Structs that does not support inheritance
members of structs cannot be specified as abstract, sealed, virtual, or protected.
References
Entenda finalmente a diferença entre struct e class — C#
Docs
Posted on November 3, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.