Home > Net >  Are Enum objects stored in stack memory?
Are Enum objects stored in stack memory?

Time:08-18

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?

CodePudding user response:

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.

  • Related