Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
URL Rewriter Bot
URL Rewriter Bot

The question has now superbly been answered by Julien LebosquainJulien Lebosquain. I would also like to extend his answer with ignoreCase, defaultValue and optional arguments, while adding TryParse and ParseOrDefault.

My old improvements on Vivek's answerVivek's answer by using the comments and 'new' developments:

The question has now superbly been answered by Julien Lebosquain. I would also like to extend his answer with ignoreCase, defaultValue and optional arguments, while adding TryParse and ParseOrDefault.

My old improvements on Vivek's answer by using the comments and 'new' developments:

The question has now superbly been answered by Julien Lebosquain. I would also like to extend his answer with ignoreCase, defaultValue and optional arguments, while adding TryParse and ParseOrDefault.

My old improvements on Vivek's answer by using the comments and 'new' developments:

Use/extend Julien Lebosquain's Enum-constraint
Source Link
Yahoo Serious
  • 4k
  • 2
  • 36
  • 38

Although yourEdit

The question about constraining T to an Enum has now superbly been answered by VivekJulien Lebosquain. I would also like to extend his answer with ignoreCase, defaultValue and the additional commentsoptional arguments, I think your resulting functionwhile adding ParseEnumTryParse can be improvedand ParseOrDefault.

public abstract class ConstrainedEnumParser<TClass> where TClass : class // value type constraint S ("TEnum") depends on reference type T ("TClass") [and on struct] { // internal constructor, to prevent this class from being inherited outside this code internal ConstrainedEnumParser() {} // Parse using pragmatic/adhoc hard cast: // - struct + class = enum // - 'guaranteed' call from derived <System.Enum>-constrained type EnumUtils public static TEnum Parse<TEnum>(string value, bool ignoreCase = false) where TEnum : struct, TClass { return (TEnum)Enum.Parse(typeof(TEnum), value, ignoreCase); } public static bool TryParse<TEnum>(string value, out TEnum result, bool ignoreCase = false, TEnum defaultValue = default(TEnum)) where TEnum : struct, TClass // value type constraint S depending on T { var didParse = Enum.TryParse(value, ignoreCase, out result); if (didParse == false) { result = defaultValue; } return didParse; } public static TEnum ParseOrDefault<TEnum>(string value, bool ignoreCase = false, TEnum defaultValue = default(TEnum)) where TEnum : struct, TClass // value type constraint S depending on T { if (string.IsNullOrEmpty(value)) { return defaultValue; } TEnum result; if (Enum.TryParse(value, ignoreCase, out result)) { return result; } return defaultValue; } } public class EnumUtils: ConstrainedEnumParser<System.Enum> // reference type constraint to any <System.Enum> { // call to parse will then contain constraint to specific <System.Enum>-class } 

Examples of usage:

WeekDay parsedDayOrArgumentException = EnumUtils.Parse<WeekDay>("monday", ignoreCase:true); WeekDay parsedDayOrDefault; bool didParse = EnumUtils.TryParse<WeekDay>("clubs", out parsedDayOrDefault, ignoreCase:true); parsedDayOrDefault = EnumUtils.ParseOrDefault<WeekDay>("friday", ignoreCase:true, defaultValue:WeekDay.Sunday); 

Old

My old improvements on Vivek's answer by thoseusing the comments and 'new' developments:

Although your question about constraining T to an Enum has been answered by Vivek and the additional comments, I think your resulting function ParseEnum can be improved by those comments and 'new' developments:

Edit

The question has now superbly been answered by Julien Lebosquain. I would also like to extend his answer with ignoreCase, defaultValue and optional arguments, while adding TryParse and ParseOrDefault.

public abstract class ConstrainedEnumParser<TClass> where TClass : class // value type constraint S ("TEnum") depends on reference type T ("TClass") [and on struct] { // internal constructor, to prevent this class from being inherited outside this code internal ConstrainedEnumParser() {} // Parse using pragmatic/adhoc hard cast: // - struct + class = enum // - 'guaranteed' call from derived <System.Enum>-constrained type EnumUtils public static TEnum Parse<TEnum>(string value, bool ignoreCase = false) where TEnum : struct, TClass { return (TEnum)Enum.Parse(typeof(TEnum), value, ignoreCase); } public static bool TryParse<TEnum>(string value, out TEnum result, bool ignoreCase = false, TEnum defaultValue = default(TEnum)) where TEnum : struct, TClass // value type constraint S depending on T { var didParse = Enum.TryParse(value, ignoreCase, out result); if (didParse == false) { result = defaultValue; } return didParse; } public static TEnum ParseOrDefault<TEnum>(string value, bool ignoreCase = false, TEnum defaultValue = default(TEnum)) where TEnum : struct, TClass // value type constraint S depending on T { if (string.IsNullOrEmpty(value)) { return defaultValue; } TEnum result; if (Enum.TryParse(value, ignoreCase, out result)) { return result; } return defaultValue; } } public class EnumUtils: ConstrainedEnumParser<System.Enum> // reference type constraint to any <System.Enum> { // call to parse will then contain constraint to specific <System.Enum>-class } 

Examples of usage:

WeekDay parsedDayOrArgumentException = EnumUtils.Parse<WeekDay>("monday", ignoreCase:true); WeekDay parsedDayOrDefault; bool didParse = EnumUtils.TryParse<WeekDay>("clubs", out parsedDayOrDefault, ignoreCase:true); parsedDayOrDefault = EnumUtils.ParseOrDefault<WeekDay>("friday", ignoreCase:true, defaultValue:WeekDay.Sunday); 

Old

My old improvements on Vivek's answer by using the comments and 'new' developments:

deleted 11 characters in body
Source Link
Yahoo Serious
  • 4k
  • 2
  • 36
  • 38

Although your question about constraining T to an Enum has been answered by Vivek and the additional comments, I think your resulting function ParseEnum can be improved by those comments and 'new' developments:

  • use TEnum for clarity for users
  • add more interface-constraints for additional constraint-checking
  • let TryParseTryParse handle ignoreCase with the existing TryParse-parameter parameter (introduced in VS2010/.Net 4)
  • optionally use the generic default value (introduced in VS2005/.Net 2)
  • use optional arguments(introduced in VS2010/.Net 4) with default values, for defaultValue and ignoreCase

resulting in:

public static class EnumUtils { public static TEnum ParseEnum<TEnum>(this string value, bool ignoreCase = true, TEnum defaultValue = default(TEnum)) where TEnum : struct, IComparable, IFormattable, IConvertible { if ( ! typeof(TEnum).IsEnum) { throw new ArgumentException("TEnum must be an enumerated type"); } if (string.IsNullOrEmpty(value)) { return defaultValue; } TEnum lResult; if (Enum.TryParse(value, ignoreCase, out lResult)) { return lResult; } return defaultValue; } } 

Although your question about constraining T to an Enum has been answered by Vivek and the additional comments, I think your resulting function ParseEnum can be improved by those comments and 'new' developments:

  • use TEnum for clarity for users
  • add more interface-constraints for additional constraint-checking
  • let TryParse handle ignoreCase with the existing TryParse-parameter (introduced in VS2010/.Net 4)
  • optionally use the generic default value (introduced in VS2005/.Net 2)
  • use optional arguments(introduced in VS2010/.Net 4) with default values, for defaultValue and ignoreCase

resulting in:

public static class EnumUtils { public static TEnum ParseEnum<TEnum>(this string value, bool ignoreCase = true, TEnum defaultValue = default(TEnum)) where TEnum : struct, IComparable, IFormattable, IConvertible { if ( ! typeof(TEnum).IsEnum) { throw new ArgumentException("TEnum must be an enumerated type"); } if (string.IsNullOrEmpty(value)) { return defaultValue; } TEnum lResult; if (Enum.TryParse(value, ignoreCase, out lResult)) { return lResult; } return defaultValue; } } 

Although your question about constraining T to an Enum has been answered by Vivek and the additional comments, I think your resulting function ParseEnum can be improved by those comments and 'new' developments:

  • use TEnum for clarity for users
  • add more interface-constraints for additional constraint-checking
  • let TryParse handle ignoreCase with the existing parameter (introduced in VS2010/.Net 4)
  • optionally use the generic default value (introduced in VS2005/.Net 2)
  • use optional arguments(introduced in VS2010/.Net 4) with default values, for defaultValue and ignoreCase

resulting in:

public static class EnumUtils { public static TEnum ParseEnum<TEnum>(this string value, bool ignoreCase = true, TEnum defaultValue = default(TEnum)) where TEnum : struct, IComparable, IFormattable, IConvertible { if ( ! typeof(TEnum).IsEnum) { throw new ArgumentException("TEnum must be an enumerated type"); } if (string.IsNullOrEmpty(value)) { return defaultValue; } TEnum lResult; if (Enum.TryParse(value, ignoreCase, out lResult)) { return lResult; } return defaultValue; } } 
fixed small lay-out issues in text and code
Source Link
Yahoo Serious
  • 4k
  • 2
  • 36
  • 38
Loading
added/moved links
Source Link
Yahoo Serious
  • 4k
  • 2
  • 36
  • 38
Loading
Source Link
Yahoo Serious
  • 4k
  • 2
  • 36
  • 38
Loading