DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 967,611 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
Cover image for Little drops on C# Interview questions - Structs x Classes
Ynoa Pedro
Ynoa Pedro

Posted on • Updated on

Little drops on C# Interview questions - Structs x Classes

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

Top comments (1)

Collapse
 
daviholandas profile image
daviholandas

Hi Ynoa,
The great job writing the drops, but I wanted to say something about the storage of structs. It's not always the structs are stored in the stack, when she is referenced by a reference type she will be stored on the heap.

🌚 Friends don't let friends browse without dark mode.

Just kidding, it's a personal preference. But you can change your theme, font, etc. in your settings.

The more you know. 🌈