Object - ToSingleOrDefault
An object extension method that converts this object to a single or default.
public static void Main() { object val1 = 123; object val2 = "1,033.67"; object thisNull = null; object thisInvalid = "A100"; string format = "{0, -20} {1, -15}"; Console.WriteLine(format, "Object", "ToSingleOrDefault"); Console.WriteLine(format, "------", "-----------------"); // C# Extension Method: Object - ToSingleOrDefault Console.WriteLine(format, val1, val1.ToSingleOrDefault()); Console.WriteLine(format, val2, val2.ToSingleOrDefault()); Console.WriteLine(format, "null", thisNull.ToSingleOrDefault()); Console.WriteLine(format, thisInvalid, thisInvalid.ToSingleOrDefault()); Console.WriteLine(format, thisInvalid, thisInvalid.ToSingleOrDefault(0)); Console.WriteLine(format, thisInvalid, thisInvalid.ToSingleOrDefault(() => 0)); }
View Source
using System; public static partial class Extensions { /// <summary> /// An object extension method that converts this object to a single or default. /// </summary> /// <param name="this">The @this to act on.</param> /// <returns>The given data converted to a float.</returns> public static float ToSingleOrDefault(this object @this) { try { return Convert.ToSingle(@this); } catch (Exception) { return default(float); } } /// <summary> /// An object extension method that converts this object to a single or default. /// </summary> /// <param name="this">The @this to act on.</param> /// <param name="defaultValue">The default value.</param> /// <returns>The given data converted to a float.</returns> public static float ToSingleOrDefault(this object @this, float defaultValue) { try { return Convert.ToSingle(@this); } catch (Exception) { return defaultValue; } } /// <summary> /// An object extension method that converts this object to a single or default. /// </summary> /// <param name="this">The @this to act on.</param> /// <param name="defaultValue">The default value.</param> /// <param name="useDefaultIfNull">true to use default if null.</param> /// <returns>The given data converted to a float.</returns> public static float ToSingleOrDefault(this object @this, float defaultValue, bool useDefaultIfNull) { if (useDefaultIfNull && @this == null) { return defaultValue; } try { return Convert.ToSingle(@this); } catch (Exception) { return defaultValue; } } /// <summary> /// An object extension method that converts this object to a single or default. /// </summary> /// <param name="this">The @this to act on.</param> /// <param name="defaultValueFactory">The default value factory.</param> /// <returns>The given data converted to a float.</returns> public static float ToSingleOrDefault(this object @this, Func<float> defaultValueFactory) { try { return Convert.ToSingle(@this); } catch (Exception) { return defaultValueFactory(); } } /// <summary> /// An object extension method that converts this object to a single or default. /// </summary> /// <param name="this">The @this to act on.</param> /// <param name="defaultValueFactory">The default value factory.</param> /// <param name="useDefaultIfNull">true to use default if null.</param> /// <returns>The given data converted to a float.</returns> public static float ToSingleOrDefault(this object @this, Func<float> defaultValueFactory, bool useDefaultIfNull) { if (useDefaultIfNull && @this == null) { return defaultValueFactory(); } try { return Convert.ToSingle(@this); } catch (Exception) { return defaultValueFactory(); } } }