7

I’m working in C# with some JSON & an API. I'm wondering how to handle something like this.

One of the JSON values is a string that can be one of the following values: “Last_Day”, “Last_Week”, “Last_Month”.

In TypeScript I can do this:

type DateSince = "Last_Day" | "Last_Week" | "Last_Month"

Then I get type hinting like this:

VS Code type hinting suggesting 3 possible values.

If the value is anything but those 3 strings, I get the red squiggle line error. My value is still technically a string as well, which is what I need to use with the JSON API requests and responses.

I have yet to find a great way to do this in C#. Is it even possible to do this in C# with relative ease?

My ideal solution lets me assign a custom type to a variable instead of using a string. This way I don't have to remember the possible string values.

Patrick Roberts
  • 49,224
  • 10
  • 102
  • 153
mattferderer
  • 894
  • 9
  • 17
  • 1
    Whether or not this is _literally_ an enumerated type is irrelevant. You have an enumeration of strings as a de facto enum, so it still applies. – Patrick Roberts Jun 29 '18 at 15:57

2 Answers2

6

As suggested by @PatrickRoberts & @afrazier, the best way is using enums and the Json.NET StringEnumConverter.

[JsonConverter(typeof(StringEnumConverter))]
public enum DateSince
{
    [EnumMember(Value = "LAST_DAY")]
    LastDay,
    [EnumMember(Value = "LAST_WEEK")]
    LastWeek,
    [EnumMember(Value = "LAST_MONTH")]
    LastMonth,
}

Customizing Enumeration Member Values

mattferderer
  • 894
  • 9
  • 17
4

In C# you can use Enums.

public enum DateSince
{
    Last_Day ,
    Last_Week,
    Last_Month
}

Usage:

var datesince = DateSince.Last_Day;

Read more about C# Enums

vendettamit
  • 14,315
  • 2
  • 32
  • 54
  • 1
    While I generally agree with this answer, it doesn't really address this particular point without more research on the asker's part: `My value is still technically a string as well, which is what I need to use with the JSON API requests and responses.` Unless you were FGITW'ing and were going to add that in an edit... – Patrick Roberts Jun 29 '18 at 15:47
  • I use enums and while they are similar they're not what I'm looking for. As Patrick states, I need a string output. With enums you need to add helpers then & I don't believe you end up with a similar result as what I'm asking for that TypeScript does in my example. Which is provide type hinting & result in a string. – mattferderer Jun 29 '18 at 15:51
  • 2
    @mattferderer you should still use enums, just use JSON.net for serialization / parsing and provide [this property attribute](https://stackoverflow.com/a/22938928/1541563) to your enum values. – Patrick Roberts Jun 29 '18 at 15:55
  • 2
    JSON.NET can take care of serializing and deserializing the enum with StringEnumConverter. – afrazier Jun 29 '18 at 15:55
  • @mattferderer if you call `.ToString()` on enum it will give you string value as it's name. e.g. `DateSince.Last_Day.ToString()` – vendettamit Jun 29 '18 at 16:10
  • @vendettamit, but at that point you need to declare the variable as a type of string & you lose type hinting. – mattferderer Jun 29 '18 at 16:14
  • 1
    Then you should go with @afrazier 's suggestion. Use enum converter for JSON serialization. – vendettamit Jun 29 '18 at 16:16
  • @PatrickRoberts Thanks. That solves my issues for my current problem since the value eventually gets serialized or deserialized before it enters or leaves my C# code. If I wasn't using JSON & serialization, would it be possible another way? – mattferderer Jun 29 '18 at 16:17
  • 2
    If you weren't using JSON and serialization, why would you still care about its string value? Refactor the rest of your code to expect an enum, it's better practice in C#. – Patrick Roberts Jun 29 '18 at 16:18