Esempio n. 1
0
 /// <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 = &quot;1&quot;;
 ///                       object invalidValue = &quot;Fizz&quot;;
 ///
 ///                       // Exemples
 ///                       var result1 = intValue.ToOrDefault&lt;int&gt;(); // return 1;
 ///                       var result2 = invalidValue.ToOrDefault&lt;int&gt;(); // return 0;
 ///                       int result3 = invalidValue.ToOrDefault(3); // return 3;
 ///                       int result4 = invalidValue.ToOrDefault(() =&gt; 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)));
 }
Esempio n. 2
0
 /// <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 = &quot;1&quot;;
 ///                       object invalidValue = &quot;Fizz&quot;;
 ///
 ///                       // Exemples
 ///                       var result1 = intValue.ToOrDefault&lt;int&gt;(); // return 1;
 ///                       var result2 = invalidValue.ToOrDefault&lt;int&gt;(); // return 0;
 ///                       int result3 = invalidValue.ToOrDefault(3); // return 3;
 ///                       int result4 = invalidValue.ToOrDefault(() =&gt; 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));
 }
Esempio n. 3
0
 /// <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 = &quot;1&quot;;
 ///                   object invalidValue = &quot;Fizz&quot;;
 ///
 ///                   // Exemples
 ///                   var result1 = intValue.ToOrDefault&lt;int&gt;(); // return 1;
 ///                   var result2 = invalidValue.ToOrDefault&lt;int&gt;(); // return 0;
 ///                   int result3 = invalidValue.ToOrDefault(3); // return 3;
 ///                   int result4 = invalidValue.ToOrDefault(() =&gt; 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 = &quot;1&quot;;
 ///                   object invalidValue = &quot;Fizz&quot;;
 ///
 ///                   // Exemples
 ///                   var result1 = intValue.ToOrDefault&lt;int&gt;(); // return 1;
 ///                   var result2 = invalidValue.ToOrDefault&lt;int&gt;(); // return 0;
 ///                   int result3 = invalidValue.ToOrDefault(3); // return 3;
 ///                   int result4 = invalidValue.ToOrDefault(() =&gt; 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 = &quot;1&quot;;
 ///                       object invalidValue = &quot;Fizz&quot;;
 ///
 ///                       // Exemples
 ///                       var result1 = intValue.ToOrDefault&lt;int&gt;(); // return 1;
 ///                       var result2 = invalidValue.ToOrDefault&lt;int&gt;(); // return 0;
 ///                       int result3 = invalidValue.ToOrDefault(3); // return 3;
 ///                       int result4 = invalidValue.ToOrDefault(() =&gt; 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 = &quot;1&quot;;
 ///                       object invalidValue = &quot;Fizz&quot;;
 ///
 ///                       // Exemples
 ///                       var result1 = intValue.ToOrDefault&lt;int&gt;(); // return 1;
 ///                       var result2 = invalidValue.ToOrDefault&lt;int&gt;(); // return 0;
 ///                       int result3 = invalidValue.ToOrDefault(3); // return 3;
 ///                       int result4 = invalidValue.ToOrDefault(() =&gt; 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()));
 }