Skip to content

Instantly share code, notes, and snippets.

@awright18
Created July 11, 2017 18:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save awright18/8b4da20bf10f73346e09711ab3aaab80 to your computer and use it in GitHub Desktop.
Save awright18/8b4da20bf10f73346e09711ab3aaab80 to your computer and use it in GitHub Desktop.
A way to avoid having to use enums
using System;
namespace NotEnums
{
public class Options
{
public string Value { get; private set; }
private const string Option1 = "Option1";
private const string Option2 = "Option2";
private const string Option3 = "Option3";
public Options(string value )
{
Value = value;
}
public static Options FirstOption = new Options(Option1);
public static Options SecondOption = new Options(Option2);
public static Options ThirdOption = new Options(Option3);
}
public class OptionsConsumer
{
public void ConsumeOptions(Options options)
{
//this is using the default equality comparer and since
//the types are static they will always be equal if the same named the same.
//You can't use switches this way, but you should try to avoid switches anyway.
if (options == Options.FirstOption)
{
//option 1
Console.WriteLine(options.Value);
}
if (options == Options.SecondOption)
{
//option 2
Console.WriteLine(options.Value);
}
if (options == Options.ThirdOption)
{
//option 3
Console.WriteLine(options.Value);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment