public static void Main()
Console.WriteLine("Tres".ToEnum<Teste>(defaultValue: "Dois"));
public static class StringExtensions
public static bool IsEmpty(this string? str)
return string.IsNullOrWhiteSpace(str);
public static T ToEnum<T>(this string value, string defaultValue = null) where T : struct
if (value.IsEmpty() && defaultValue.IsEmpty())
return Enum.TryParse(value.GetValueOrDefault(defaultValue), true, out T result) ? result : default(T);
public static string? GetValueOrDefault(this string? str, string? defaultValue)
return str.IsNotNullOrEmpty() ? str : defaultValue;
public static string? GetValueOrDefault(this string? str, Func<string?> defaultValue)
return str.IsNotEmpty() ? str : defaultValue();
public static bool IsNotEmpty(this string str)
return !string.IsNullOrWhiteSpace(str);
public static bool IsNotNullOrEmpty(this object obj) =>
obj is not null && obj.ToString().IsNotEmpty();