Click here to Skip to main content
15,790,565 members
Home / Discussions / C#
   

C#

 
AnswerRe: Maintain the Runing balance in Datagridview Pin
ZurdoDev21-Apr-20 10:51
professionalZurdoDev21-Apr-20 10:51 
Questionextending an Enum with a function that returns a generic value ? Pin
BillWoodruff20-Apr-20 9:58
professionalBillWoodruff20-Apr-20 9:58 
AnswerRe: extending an Enum with a function that returns a generic value ? Pin
Richard Deeming20-Apr-20 10:34
mveRichard Deeming20-Apr-20 10:34 
GeneralRe: extending an Enum with a function that returns a generic value ? Pin
BillWoodruff20-Apr-20 12:56
professionalBillWoodruff20-Apr-20 12:56 
GeneralRe: extending an Enum with a function that returns a generic value ? Pin
BillWoodruff20-Apr-20 23:10
professionalBillWoodruff20-Apr-20 23:10 
GeneralRe: extending an Enum with a function that returns a generic value ? Pin
Richard Deeming21-Apr-20 1:01
mveRichard Deeming21-Apr-20 1:01 
GeneralRe: extending an Enum with a function that returns a generic value ? Pin
BillWoodruff21-Apr-20 5:30
professionalBillWoodruff21-Apr-20 5:30 
GeneralRe: extending an Enum with a function that returns a generic value ? Pin
Richard Deeming21-Apr-20 6:17
mveRichard Deeming21-Apr-20 6:17 
If you make the parameter TEnum, you won't need to check whether it's the same type as the type parameter, and you won't need to repeat the type when you call it.
C#
public static Func<string, bool, TEnum> MakeConverter<TEnum>(this TEnum tenum) ...
var econverter = WeekDays.Default.MakeConverter();
I don't think you need to worry about getting an exception when you create the function - anything that's thrown then won't be recoverable anyway.

You can also let the compiler infer the parameter types for the returned Func<>.
C#
public static Func<string, bool, TEnum> MakeConverter<TEnum>(this TEnum tenum)
    where TEnum : struct, Enum
{
    return (str, ignoreCase) =>
    {
        if (Enum.TryParse<TEnum>(str, ignoreCase, out var result)) return result;
        throw new ArgumentException($"'{str}' is not a valid string for conversion to {typeof(TEnum).FullName}");
    };
}
With a throw expression[^], the converter function can come down to a single line:
C#
return (str, ignoreCase) => Enum.TryParse<TEnum>(str, ignoreCase, out var result) ? result
    : throw new ArgumentException($"'{str}' is not a valid string for conversion to {typeof(TEnum).FullName}");
You could even make the factory method an expression-bodied method, although I think that might be taking things a little too far:
C#
public static Func<string, bool, TEnum> MakeConverter<TEnum>(this TEnum tenum) where TEnum : struct, Enum
   => (str, ignoreCase) => Enum.TryParse<TEnum>(str, ignoreCase, out var result) ? result
    : throw new ArgumentException($"'{str}' is not a valid string for conversion to {typeof(TEnum).FullName}");

NB: If you want to maintain the optional parameter, you can't use the generic Func<> delegate; you'd need a custom delegate type:
C#
public delegate TEnum EnumConverterDelegate<TEnum>(string str, bool ignoreCase = false) where TEnum : struct, Enum;

public static EnumConverterDelegate<TEnum> MakeConverter<TEnum>(this TEnum value) where TEnum : struct, Enum
   => (str, ignoreCase) => Enum.TryParse<TEnum>(str, ignoreCase, out var result) ? result
    : throw new ArgumentException($"'{str}' is not a valid string for conversion to {typeof(TEnum).FullName}");




"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer

GeneralRe: extending an Enum with a function that returns a generic value ? Pin
BillWoodruff21-Apr-20 9:29
professionalBillWoodruff21-Apr-20 9:29 
AnswerRe: extending an Enum with a function that returns a generic value ? Pin
Matthew Dennis21-Apr-20 6:36
sysadminMatthew Dennis21-Apr-20 6:36 
GeneralRe: extending an Enum with a function that returns a generic value ? Pin
BillWoodruff21-Apr-20 9:27
professionalBillWoodruff21-Apr-20 9:27 
GeneralRe: extending an Enum with a function that returns a generic value ? Pin
Richard Deeming21-Apr-20 9:37
mveRichard Deeming21-Apr-20 9:37 
GeneralRe: extending an Enum with a function that returns a generic value ? Pin
BillWoodruff21-Apr-20 9:59
professionalBillWoodruff21-Apr-20 9:59 
GeneralRe: extending an Enum with a function that returns a generic value ? Pin
Matthew Dennis21-Apr-20 10:16
sysadminMatthew Dennis21-Apr-20 10:16 
GeneralRe: extending an Enum with a function that returns a generic value ? Pin
BillWoodruff21-Apr-20 21:57
professionalBillWoodruff21-Apr-20 21:57 
GeneralRe: extending an Enum with a function that returns a generic value ? Pin
Matthew Dennis22-Apr-20 5:35
sysadminMatthew Dennis22-Apr-20 5:35 
GeneralRe: extending an Enum with a function that returns a generic value ? Pin
BillWoodruff22-Apr-20 7:12
professionalBillWoodruff22-Apr-20 7:12 
GeneralRe: extending an Enum with a function that returns a generic value ? Pin
BillWoodruff21-Apr-20 22:53
professionalBillWoodruff21-Apr-20 22:53 
GeneralRe: extending an Enum with a function that returns a generic value ? Pin
Richard Deeming22-Apr-20 0:25
mveRichard Deeming22-Apr-20 0:25 
GeneralRe: extending an Enum with a function that returns a generic value ? Pin
BillWoodruff22-Apr-20 4:01
professionalBillWoodruff22-Apr-20 4:01 
AnswerRe: extending an Enum with a function that returns a generic value ? Pin
#realJSOP22-Apr-20 8:25
mve#realJSOP22-Apr-20 8:25 
GeneralRe: extending an Enum with a function that returns a generic value ? Pin
BillWoodruff22-Apr-20 8:39
professionalBillWoodruff22-Apr-20 8:39 
GeneralRe: extending an Enum with a function that returns a generic value ? Pin
#realJSOP22-Apr-20 8:47
mve#realJSOP22-Apr-20 8:47 
GeneralRe: extending an Enum with a function that returns a generic value ? Pin
BillWoodruff22-Apr-20 21:31
professionalBillWoodruff22-Apr-20 21:31 
GeneralRe: extending an Enum with a function that returns a generic value ? Pin
#realJSOP23-Apr-20 1:58
mve#realJSOP23-Apr-20 1:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.