<p>Are there any good solutions to represent a parameterized enum in <code>C# 3.0</code>? I am looking for something like <a href="http://www.ocaml.org" rel="nofollow noreferrer">OCaml</a> or <a href="http://www.haxe.org" rel="nofollow noreferrer">Haxe</a> has. I can only think of class hierarchy with a simple enum field for easy switching for now, maybe there are better ideas?</p>

<p>See Ocaml example below in one of the replies, a Haxe code follows:</p>

<pre><code>enum Tree {
   Node(left: Tree, right: Tree);
   Leaf(val: Int);
}
</code></pre>

[{'answer_id': 66895, 'author': 'MagicKat', 'author_id': 8505, 'author_profile': 'https://Stackoverflow.com/users/8505', 'pm_score': 0, 'selected': False, 'text': "<p>C# (the .NET framework in general, as far as I know) doesn't support parametrized enums like Java does.  That being said, you might want to look at Attributes.  Some of the features that Java enums are capable of are somewhat doable through Attributes.</p>\n"}, {'answer_id': 67042, 'author': 'FlySwat', 'author_id': 1965, 'author_profile': 'https://Stackoverflow.com/users/1965', 'pm_score': 0, 'selected': False, 'text': "<p>What's wrong with just using a class for this? Its ugly, but thats how the Java people did it until they had language integrated Enum support!</p>\n"}, {'answer_id': 67321, 'author': 'Orion Edwards', 'author_id': 234, 'author_profile': 'https://Stackoverflow.com/users/234', 'pm_score': 4, 'selected': True, 'text': '<p>Not being familiar with OCaml or Haxe, and not being clever enough to understand the other explanations, I went and looked up the <a href="https://haxe.org/manual/types-enum-instance.html" rel="nofollow noreferrer">Haxe enum documentation</a> - the \'Enum Type Parameters\' bit at the bottom seems to be the relevant part.</p>\n\n<p>My understanding based on that is as follows:</p>\n\n<p>A \'normal\' enum is basically a value which is restricted to the things that you have defined in your enum definition. C# Example:</p>\n\n<pre><code>enum Color{ Red, Green, Yellow, Blue };\nColor c = Color.Red;\n</code></pre>\n\n<p><code>c</code> can either be <code>Red</code>, <code>Green</code>, <code>Yellow</code>, or <code>Blue</code>, but nothing else.</p>\n\n<p>In Haxe, you can add complex types to enums, Contrived example from their page:</p>\n\n<pre><code>enum Cell&lt;T&gt;{ \n  empty; \n  cons( item : T, next : Cell&lt;T&gt; )\n}\n\nCell&lt;int&gt; c = &lt;I don\'t know&gt;;\n</code></pre>\n\n<p>What this <em>appears</em> to mean is that <code>c</code> is restricted to either being the literal value <code>empty</code> (like our old fashioned C# enums), or it can also be a complex type <code>cons(item, next)</code>, where <code>item</code> is a <code>T</code> and <code>next</code> is a <code>Cell&lt;T&gt;</code>.</p>\n\n<p>Not having ever used this it looks like it is probably generating some anonymous types (like how the C# compiler does when you do <code>new { Name=\'Joe\'}</code>.<br>\nWhenever you \'access\' the enum value, you have to declare <code>item</code> and <code>next</code> when you do so, and it looks like they get bound to temporary local variables.</p>\n\n<p>Haxe example - You can see \'next\' being used as a temporary local variable to pull data out of the anonymous cons structure:</p>\n\n<pre><code>switch( c ) {\n  case empty : 0;\n  case cons(item,next): 1 + cell_length(next);\n}\n</code></pre>\n\n<p>To be honest, this blew my mind when I \'clicked\' onto what it seemed to be doing. It seems incredibly powerful, and I can see why you\'d be looking for a similar feature in C#.</p>\n\n<p>C# enums are pretty much the same as C/++ enums from which they were originally copied. It\'s basically a nice way of saying <code>#define Red 1</code> so the compiler can do comparisons and storage with integers instead of strings when you are passing <code>Color</code> objects around.</p>\n\n<p>My stab at doing this in C# would be to use generics and interfaces. Something like this:</p>\n\n<pre><code>public interface ICell&lt;T&gt; {\n   T Item{ get; set; }\n   ICell&lt;T&gt;{ get; set; }\n}\n\nclass Cons&lt;T&gt; : ICell&lt;T&gt; {\n  public T Item{ get; set; } /* C#3 auto-backed property */\n  public Cell&lt;T&gt; Next{ get; set; }\n}\n\nclass EmptyCell&lt;T&gt; : ICell&lt;T&gt;{\n  public T Item{ get{ return default(T); set{ /* do nothing */ }; }\n  public ICell&lt;T&gt; Next{ get{ return null }; set{ /* do nothing */; }\n}\n</code></pre>\n\n<p>Then you could have a <code>List&lt;ICell&lt;T&gt;&gt;</code> which would contain items and next cell, and you could insert <code>EmptyCell</code> at the end (or just have the <code>Next</code> reference explicitly set to null).\nThe advantages would be that because <code>EmptyCell</code> contains no member variables, it wouldn\'t require any storage space (like the <code>empty</code> in Haxe), whereas a <code>Cons</code> cell would.<br>\nThe compiler may also inline / optimize out the methods in <code>EmptyCell</code> as they do nothing, so there may be a speed increase over just having a <code>Cons</code> with it\'s member data set to null.</p>\n\n<p>I don\'t really know. I\'d welcome any other possible solutions as I\'m not particularly proud of my one :-)</p>\n'}, {'answer_id': 68959, 'author': 'user10834', 'author_id': 10834, 'author_profile': 'https://Stackoverflow.com/users/10834', 'pm_score': 1, 'selected': False, 'text': '<p>Use a <strong>class</strong> that with static properties to represent the enumeration values.  You can, optionally, use a private constructor to force all references to the class to go through a static property.  </p>\n\n<p>Take a look at the <code>System.Drawing.Color</code> class.  It uses this approach.</p>\n'}]
['https://Stackoverflow.com/questions/66819', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/9777/']