简体   繁体   中英

Confusion when casting string value to enum using generic and simply casting string value to enum

I am trying to understand generics and while i was watching 1 pluralsight video of SCOTT ALLEN on Generics ,he showed example of ugly code vs good code but i didnt understood something which i would like to mention below :

public enum Steps
{
  Step1,
  Step2,
  Step3
}

Example of Ugly code :

Steps value = (Steps)Enum.Parse(typeof(Steps),input);

Good code and strongly typed :

public static class StringExtensions
{
   public static TEnum ParseEnum<TEnum>(this string value)
   {
      return (TEnum)Enum.Parse(typeof(TEnum),value);
   }
}

var input = "Step1";
var value = input.ParseEnum<Steps>();
console.writeline(value);

But here i dont understand why second code is good as it is also doing a casting and seems like code 1 and 2 is same or may be I have not properly understood why second code is better as it is doing same type casting.

Can someone please explain how second code is strongly type and better as per Author although it is doing same casting as first code snippet?

I think this is a bad example. The only reason I think the second code snippet is 'good code' is that it conforms to the DRY principle (Do Not Repeat Yourself). This is generic code that code be written a 100 times, but now done once and done good.

For the generics part, there is quite some wrong with this code. You can call it on types not being an enum for example. And it doesn't really show the benefits of generics. A much simpler example would have done.

Good code and strongly typed

That's not good code, and there's nothing more strongly typed than in the first example. Even more, with that, IntelliSense would suggest me that

var name = "@CamiloTerevinto@".ParseEnum<Fruit>();

is valid, when it clearly isn't. In fact, that code doesn't even have a try-catch for a scenario like that.

Overall, the second snippet is somewhat better only to comply with DRY, but I wouldn't make it an extension method nor call it strongly typed.

I would say the extension method provides better readability when used and I guess that is what Scott Allen was trying to show an example of. Did he really say it is more strongly typed?

The DRY principle is another good point for putting it inside a method.

Also I would probably extend it with a check for valid string values, such as:

public static TEnum ParseEnum<TEnum>(this string enumValue, bool ignoreCase = true) where TEnum : struct
{
    if (!Enum.TryParse(enumValue, ignoreCase, out TEnum result))
    {
        throw new ArgumentException(
            $"{enumValue} is not a valid {typeof(TEnum).Name}",
            typeof(TEnum).Name);
    }

    return result;
}

...but that is beside the point.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM