String - NullIfEmpty
A string extension method that return null if the value is empty else the value.
public static void Main() { string [] values = {"EntityFramework", "", "SQL", "Java"}; foreach(var str in values) { // C# Extension Method: String - NullIfEmpty string newStr = str.NullIfEmpty(); Console.WriteLine(newStr); } }
View Source
public static partial class Extensions { /// <summary> /// A string extension method that return null if the value is empty else the value. /// </summary> /// <param name="this">The @this to act on.</param> /// <returns>null if the value is empty, otherwise the value.</returns> public static string NullIfEmpty(this string @this) { return @this == "" ? null : @this; } }