Intellenum Help

Create and use Intellenum enums

The tutorial on "your first enum" introduced you to some of the different ways to create an Intellenum.

In this how-to article, we'll explore the rest. The different ways to create instances are:

  • Field declarations

  • the Member attribute

  • the Members (plural) attribute

  • The Member method

  • The Members (plural) method

Let's look at each way:

Field declarations

[Intellenum<string>] public partial class CustomerType { public static readonly CustomerType Standard, Silver, Gold; }

This produces an instance with three members.

Console.WriteLine(CustomerType.Standard); // "Standard" Console.WriteLine(CustomerType.Silver); // "Silver" Console.WriteLine(CustomerType.Gold); // "Gold"

You can also use different values, e.g.

[Intellenum<string>] public partial class CustomerType { public static readonly CustomerType Standard = new("STD"), Silver = new("SLVR"), Gold = new ("GLD"); }

The above examples are for enums based on string. The values are defaulted to be the same as name.

For int-based enums, values are defaulted start at zero and increment by one:

[Intellenum<int>] public partial class CustomerType { public static readonly CustomerType Standard, Silver, Gold; }

This produces an instance with three members.

Console.WriteLine(CustomerType.Standard); // 0 Console.WriteLine(CustomerType.Silver); // 1 Console.WriteLine(CustomerType.Gold); // 2

Attributes

You can use attributes to declare members, e.g.

[Intellenum<string>] [Members("Standard, Gold, Silver")] public partial class CustomerType;

... or individually

[Intellenum<string>] [Member("Standard")] [Member("Gold")] [Member("Silver")] public partial class CustomerType;

Static constructors

You can declare members via the Members and Member methods from a static constructor:

[Intellenum<string> public partial class CustomerType { static CustomerType() => Members("Standard, Silver, Gold"); }
Last modified: 12 September 2024