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?