/// <summary> /// A System.Object extension method that converts this object to an or default. /// </summary> /// <typeparam name="T">Generic type parameter.</typeparam> /// <param name="this">this.</param> /// <returns>The given data converted to a T.</returns> /// <example> /// <code> /// using Microsoft.VisualStudio.TestTools.UnitTesting; /// using Z.ExtensionMethods.Object; /// /// namespace ExtensionMethods.Examples /// { /// [TestClass] /// public class System_Object_ToOrDefault /// { /// [TestMethod] /// public void ToOrDefault() /// { /// // Type /// object intValue = "1"; /// object invalidValue = "Fizz"; /// /// // Exemples /// var result1 = intValue.ToOrDefault<int>(); // return 1; /// var result2 = invalidValue.ToOrDefault<int>(); // return 0; /// int result3 = invalidValue.ToOrDefault(3); // return 3; /// int result4 = invalidValue.ToOrDefault(() => 4); // return 4; /// /// // Unit Test /// Assert.AreEqual(1, result1); /// Assert.AreEqual(0, result2); /// Assert.AreEqual(3, result3); /// Assert.AreEqual(4, result4); /// } /// } /// } /// </code> /// </example> public static T ToOrDefault <T>(this System.Object @this) { return(@this.ToOrDefault(x => default(T))); }
/// <summary> /// A System.Object extension method that converts this object to an or default. /// </summary> /// <typeparam name="T">Generic type parameter.</typeparam> /// <param name="this">this.</param> /// <param name="defaultValue">The default value.</param> /// <returns>The given data converted to a T.</returns> /// <example> /// <code> /// using Microsoft.VisualStudio.TestTools.UnitTesting; /// using Z.ExtensionMethods.Object; /// /// namespace ExtensionMethods.Examples /// { /// [TestClass] /// public class System_Object_ToOrDefault /// { /// [TestMethod] /// public void ToOrDefault() /// { /// // Type /// object intValue = "1"; /// object invalidValue = "Fizz"; /// /// // Exemples /// var result1 = intValue.ToOrDefault<int>(); // return 1; /// var result2 = invalidValue.ToOrDefault<int>(); // return 0; /// int result3 = invalidValue.ToOrDefault(3); // return 3; /// int result4 = invalidValue.ToOrDefault(() => 4); // return 4; /// /// // Unit Test /// Assert.AreEqual(1, result1); /// Assert.AreEqual(0, result2); /// Assert.AreEqual(3, result3); /// Assert.AreEqual(4, result4); /// } /// } /// } /// </code> /// </example> public static T ToOrDefault <T>(this System.Object @this, T defaultValue) { return(@this.ToOrDefault(x => defaultValue)); }
/// <summary> /// A System.Object extension method that converts this object to an or default. /// </summary> /// <typeparam name="T">Generic type parameter.</typeparam> /// <param name="this">this.</param> /// <param name="defaultValueFactory">The default value factory.</param> /// <returns>The given data converted to a T.</returns> /// <example> /// <code> /// using Microsoft.VisualStudio.TestTools.UnitTesting; /// using Z.ExtensionMethods.Object; /// /// namespace ExtensionMethods.Examples /// { /// [TestClass] /// public class System_Object_ToOrDefault /// { /// [TestMethod] /// public void ToOrDefault() /// { /// // Type /// object intValue = "1"; /// object invalidValue = "Fizz"; /// /// // Exemples /// var result1 = intValue.ToOrDefault<int>(); // return 1; /// var result2 = invalidValue.ToOrDefault<int>(); // return 0; /// int result3 = invalidValue.ToOrDefault(3); // return 3; /// int result4 = invalidValue.ToOrDefault(() => 4); // return 4; /// /// // Unit Test /// Assert.AreEqual(1, result1); /// Assert.AreEqual(0, result2); /// Assert.AreEqual(3, result3); /// Assert.AreEqual(4, result4); /// } /// } /// } /// </code> /// </example> /// <example> /// <code> /// using Microsoft.VisualStudio.TestTools.UnitTesting; /// using Z.ExtensionMethods.Object; /// /// namespace ExtensionMethods.Examples /// { /// [TestClass] /// public class System_Object_ToOrDefault /// { /// [TestMethod] /// public void ToOrDefault() /// { /// // Type /// object intValue = "1"; /// object invalidValue = "Fizz"; /// /// // Exemples /// var result1 = intValue.ToOrDefault<int>(); // return 1; /// var result2 = invalidValue.ToOrDefault<int>(); // return 0; /// int result3 = invalidValue.ToOrDefault(3); // return 3; /// int result4 = invalidValue.ToOrDefault(() => 4); // return 4; /// /// // Unit Test /// Assert.AreEqual(1, result1); /// Assert.AreEqual(0, result2); /// Assert.AreEqual(3, result3); /// Assert.AreEqual(4, result4); /// } /// } /// } /// </code> /// </example> /// <example> /// <code> /// using Microsoft.VisualStudio.TestTools.UnitTesting; /// using Z.ExtensionMethods.Object; /// /// namespace ExtensionMethods.Examples /// { /// [TestClass] /// public class System_Object_ToOrDefault /// { /// [TestMethod] /// public void ToOrDefault() /// { /// // Type /// object intValue = "1"; /// object invalidValue = "Fizz"; /// /// // Exemples /// var result1 = intValue.ToOrDefault<int>(); // return 1; /// var result2 = invalidValue.ToOrDefault<int>(); // return 0; /// int result3 = invalidValue.ToOrDefault(3); // return 3; /// int result4 = invalidValue.ToOrDefault(() => 4); // return 4; /// /// // Unit Test /// Assert.AreEqual(1, result1); /// Assert.AreEqual(0, result2); /// Assert.AreEqual(3, result3); /// Assert.AreEqual(4, result4); /// } /// } /// } /// </code> /// </example> /// <example> /// <code> /// using Microsoft.VisualStudio.TestTools.UnitTesting; /// using Z.ExtensionMethods.Object; /// /// namespace ExtensionMethods.Examples /// { /// [TestClass] /// public class System_Object_ToOrDefault /// { /// [TestMethod] /// public void ToOrDefault() /// { /// // Type /// object intValue = "1"; /// object invalidValue = "Fizz"; /// /// // Exemples /// var result1 = intValue.ToOrDefault<int>(); // return 1; /// var result2 = invalidValue.ToOrDefault<int>(); // return 0; /// int result3 = invalidValue.ToOrDefault(3); // return 3; /// int result4 = invalidValue.ToOrDefault(() => 4); // return 4; /// /// // Unit Test /// Assert.AreEqual(1, result1); /// Assert.AreEqual(0, result2); /// Assert.AreEqual(3, result3); /// Assert.AreEqual(4, result4); /// } /// } /// } /// </code> /// </example> public static T ToOrDefault <T>(this System.Object @this, Func <T> defaultValueFactory) { return(@this.ToOrDefault(x => defaultValueFactory())); }