1
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?

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
Jason9789
  • 295
  • 1
  • 9
  • It is not very clear what your question actually is. To answer the question in title, enums are value types and allocation depends on the context. For instance, local enum variables are allocated on the stack while enum class fields are allocated in the heap. – vc 74 Aug 17 '22 at 19:11
  • 1
    Target-typed new expressions set the value of the variable to the default value of the underlying type (if the type can't be instantiated). That being said, by default, the underlying type of an enum is `int`. The default value of an `int` is 0. Essentially all that's happening in your first example is since you can't instantiate an `enum`, it's getting the default value of an `int` and setting it to that, which will assign the property with the value of 0, which, unless explicitly set, is always the first option. – Jesse Aug 17 '22 at 19:16
  • @Jesse, thank you for explenation. So ehn I say Engines uuu = new(); or Engines uuu2 = Engines.Rolls_Royce__Trent_7000; I am not instantiating the enum. What am I doing then? – Jason9789 Aug 17 '22 at 19:23
  • 1
    All you're doing is assigning a value the variable. All enum properties are constants. – Jesse Aug 17 '22 at 19:29
  • Does this answer your question? [Enum is Reference Type or Value Type?](https://stackoverflow.com/questions/14561338/enum-is-reference-type-or-value-type) – Charlieface Aug 17 '22 at 21:10

1 Answers1

1

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.

Marc Costello
  • 439
  • 1
  • 3
  • 12
  • thank you. So the items of enums , basiaclly everything is on stack? – Jason9789 Aug 17 '22 at 19:24
  • 2
    [The Stack Is An Implementation Detail](https://ericlippert.com/2009/04/27/the-stack-is-an-implementation-detail-part-one/) – madreflection Aug 17 '22 at 19:25
  • So whn I say Engines uuu2 = Engines.Rolls_Royce__Trent_7000; it is like saying int something= int something right? No object is being created. @madreflection – Jason9789 Aug 17 '22 at 19:27
  • 1
    Essentially, yes. At least in that nothing is being allocated on the managed heap. One could argue that an enum instance is an object of sorts, to the same extent that an `int` is an object. It depends on how deep you buy into the abstraction at a given point in time. – madreflection Aug 17 '22 at 19:28
  • 2
    `Enums are integers, which will live on the stack, providing you don't require them to be boxed` Not necessarily, a class int field lives on the heap and is not boxed for instance – vc 74 Aug 17 '22 at 19:28
  • 1
    @vc74 As mentioned in the comment above, the stack is an implementation detail. It might not even exist in a conforming implementation, although none exist that do this at the moment. Equally the heap might not exist either: my fully conforming implementation of .Net stores variables on the moon, which is made of cheese. – Charlieface Aug 17 '22 at 20:57
  • @Charlieface An implementation detail maybe but a very leaky one in this case, why do we have structs and classes for instance? – vc 74 Sep 13 '22 at 17:54
  • @vc74 That may be, but it's not really relevant to the question. What OP is really asking is about *semantics* not *implementation* ie the semantics of structs which are copy-by-value, and the semantics of classes which are copy-by-reference. Enums are structs, and have the same semantics, the whole "stack" thing just isn't relevant (other than that OP for some reason stuffed it into the title) – Charlieface Sep 13 '22 at 19:54