1

I'm still at my early beginning in C# and I'm trying to implement Enum/converter converters.

All my enums has a NotDefined value equals to -1.

I would like to implement a generic extension method to get an enum from a string base on the the enum description (which mostly differ in spaces and cases).

public static T ToEnum<T>(this string value) where T : struct 
{
    foreach (T e in Enum.GetValues(typeof(T)))
    {
        if (e.GetDescription() ==value)
        {
            return e;
        }
    }
    
    // Doesn't work..
    // How can I return T.NotDefined? - I don't really want to throw something here
    return (T)-1; 
}

PS I initially found the GetDescription() implementation thanks to this thread : Enum ToString with user friendly strings which helps me a lot :-) .

Does anyone have an insight about my issue?

Many thanks!

Ludovic Wagner
  • 115
  • 2
  • 8
  • You can return Nullable instead of T to solve this case: https://stackoverflow.com/questions/1165402/initial-value-of-an-enum – Ava_Katushka Sep 24 '20 at 17:52
  • I think what you're looking for is `(T)Enum.ToObject(typeof(T), -1)` . – madreflection Sep 24 '20 at 17:55
  • Thanks for your quick replies, I eventually also came the `default` keyword thanks to Google.. – Ludovic Wagner Sep 24 '20 at 17:59
  • `default` will give you `(T)0`, not `(T)-1`. – madreflection Sep 24 '20 at 17:59
  • Yeah, you're absolutely right. I thought that I had written "but have to modify the NotDefined value to 0" indeed... – Ludovic Wagner Sep 24 '20 at 18:16
  • All *Value Types* (which include enums) have a default value of 0 (or whatever 0 means in the context of the type). Floats and doubles default to 0.0, enums to 0 (even if 0 is not defined for the type), DateTimes to "time zero on date zero". – Flydog57 Sep 24 '20 at 19:53
  • Try changing your `where` specification on the type. Recent versions of C# (I think starting at v7.3) allow you to say `where T : Enum`. – Flydog57 Sep 24 '20 at 19:56
  • @Flydog57: Quite true, but it's unrelated to the fact that OP wanted the `-1` value of an arbitrary enum type. Anyway, you would need both: `where T : struct, Enum` because `struct` ensures that it's sealed. Otherwise, they could specify the abstract `Enum` type as the type argument and that would involve --gasp!-- *boxing*. – madreflection Sep 24 '20 at 21:49
  • @madreflection: Really! Damn!. For what it's worth, this works (though I don't advocate it): `public static T TestEnumDefaults() where T: Enum { return (T)Enum.Parse(typeof(T), Enum.GetName(typeof(T), -1)); }` You'll need null checking and a decision about what to do when it fails – Flydog57 Sep 25 '20 at 00:37
  • @Flydog57: That's another reason to use `Enum.ToObject`... you don't have to have a member with the `-1` value. If there's no member with that value, you'll still get an instance, just like you would with a direct cast such as `(AttributeTargets)(-1)`. – madreflection Sep 25 '20 at 01:03
  • Thanks, I agree that nothing really agrees me not to forget adding the `NotDefined = -1` enum value for future enums. Not so confortable with sealed class yet, but I also came accross some more accurate generic constraints. The problem with `where Enum` is that is doesn't recognize the extension method `GetDescription()` for some reasons... – Ludovic Wagner Sep 25 '20 at 14:27
  • You need to change `GetDescription` to use `where T : struct, Enum`, too. That post predates the change to the language that allows for the `Enum` part of the constraint. – madreflection Sep 25 '20 at 20:28
  • OK, I made the change to all my generic enum methods and it works well. – Ludovic Wagner Sep 26 '20 at 06:46

0 Answers0