Closed kurnakovv closed 2 months ago
Hi
@kurnakovv Hey, why do we sometimes use TAnotherEnum and sometimes TEnum, what's the difference, why do we need two?
@Kurnakova it depends on an input parameter
TAnotherEnum
- when you convert input enum to another enum
TEnum
- when you convert input string to enum
In your cases you need to use TEnum
, because input parameter is string (not enum)
I hope it's clear
P.S. Of course you can use TEnum
for both cases, or just T
, but I think my code is better
@kurnakovv
Returns "TAnotherEnum" or "defaultValue" if not possible to convert.
Why does say that it returns TAnotherEnum though? Or is this a typo?
Good catch, fixed
@kurnakovv
Code Examples bool enumFirst = stringValueFirst.ToEnumOrDefault
();
Why is it bool? Shouldn't it be "MyEnum" or smth like that, since this method returns enums?
@Kurnakova yep, good catch, thank you! I fixed
ToEnumOrDefault
Description
Try convert string value to enum or return default value if not possible.
Signature
bool ToEnumOrDefault(this string, bool [default = true], TEnum [default = TEnum]) where TEnum : struct, Enum
Type Parameters
TEnum
- Enum that we want try to get after convert.Parameters
string stringValue
- Input string we want to convert toTEnum
.bool ignoreCase
- Ignore or regard case.TEnum defaultValue
-TEnum
or default ofTEnum
.Returns
TEnum
ordefaultValue
if not possible to convert.Code examples