Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Are Enum objects stored in stack memory?

enum Engines { CFM, Pratt_And_Whitney, GE9X, Rolls_Royce__Trent_7000}

//Main method
        Engines uuu = new();
        Engines uuu2 = Engines.Rolls_Royce__Trent_7000;

        Console.WriteLine(uuu);
        Console.WriteLine(uuu2);

I am asking this because I am trying to understand what represents an enum type object. I mean when I say Engines uuu = new(); and print it out, it prints the first variable(item) in the enum and when I say Engines uuu2 = Engines.Rolls_Royce__Trent_7000; obviously it prints out the corresponding variable (Item).

This is confusing also because, how can I create an enum variable and assign it a static variable like this?:

Engines uuu2 = Engines.Rolls_Royce__Trent_7000;

Can you help me understand how things work here?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Enums are integers, which will live on the stack, providing you don’t require them to be boxed (by adding them to an object field for example).

It’s best to think of a C# enum as an integer with a named alias.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading