Object - GetValueOrDefault
A T extension method that gets value or default.
public static void Main() { var xml = new XmlDocument(); // C# Extension Method: Object - GetValueOrDefault Console.WriteLine(xml.GetValueOrDefault(x => x.FirstChild.InnerXml, "FizzBuzz")); // return "FizzBuzz"; Console.WriteLine(xml.GetValueOrDefault(x => x.FirstChild.InnerXml, () => "FizzBuzz")); // return "FizzBuzz" }
View Source
using System; public static partial class Extensions { /// <summary> /// A T extension method that gets value or default. /// </summary> /// <typeparam name="T">Generic type parameter.</typeparam> /// <typeparam name="TResult">Type of the result.</typeparam> /// <param name="this">The @this to act on.</param> /// <param name="func">The function.</param> /// <returns>The value or default.</returns> public static TResult GetValueOrDefault<T, TResult>(this T @this, Func<T, TResult> func) { try { return func(@this); } catch (Exception) { return default(TResult); } } /// <summary> /// A T extension method that gets value or default. /// </summary> /// <typeparam name="T">Generic type parameter.</typeparam> /// <typeparam name="TResult">Type of the result.</typeparam> /// <param name="this">The @this to act on.</param> /// <param name="func">The function.</param> /// <param name="defaultValue">The default value.</param> /// <returns>The value or default.</returns> public static TResult GetValueOrDefault<T, TResult>(this T @this, Func<T, TResult> func, TResult defaultValue) { try { return func(@this); } catch (Exception) { return defaultValue; } } /// <summary> /// A T extension method that gets value or default. /// </summary> /// <typeparam name="T">Generic type parameter.</typeparam> /// <typeparam name="TResult">Type of the result.</typeparam> /// <param name="this">The @this to act on.</param> /// <param name="func">The function.</param> /// <param name="defaultValueFactory">The default value factory.</param> /// <returns>The value or default.</returns> /// <example> /// <code> /// using System.Xml; /// using Microsoft.VisualStudio.TestTools.UnitTesting; /// /// /// namespace ExtensionMethods.Examples /// { /// [TestClass] /// public class System_Object_GetValueOrDefault /// { /// [TestMethod] /// public void GetValueOrDefault() /// { /// // Type /// var @this = new XmlDocument(); /// /// // Exemples /// string result1 = @this.GetValueOrDefault(x => x.FirstChild.InnerXml, "FizzBuzz"); // return "FizzBuzz"; /// string result2 = @this.GetValueOrDefault(x => x.FirstChild.InnerXml, () => "FizzBuzz"); // return "FizzBuzz" /// /// // Unit Test /// Assert.AreEqual("FizzBuzz", result1); /// Assert.AreEqual("FizzBuzz", result2); /// } /// } /// } /// </code> /// </example> /// <example> /// <code> /// using System.Xml; /// using Microsoft.VisualStudio.TestTools.UnitTesting; /// using Z.ExtensionMethods.Object; /// /// namespace ExtensionMethods.Examples /// { /// [TestClass] /// public class System_Object_GetValueOrDefault /// { /// [TestMethod] /// public void GetValueOrDefault() /// { /// // Type /// var @this = new XmlDocument(); /// /// // Exemples /// string result1 = @this.GetValueOrDefault(x => x.FirstChild.InnerXml, "FizzBuzz"); // return "FizzBuzz"; /// string result2 = @this.GetValueOrDefault(x => x.FirstChild.InnerXml, () => "FizzBuzz"); // return "FizzBuzz" /// /// // Unit Test /// Assert.AreEqual("FizzBuzz", result1); /// Assert.AreEqual("FizzBuzz", result2); /// } /// } /// } /// </code> /// </example> /// <example> /// <code> /// using System.Xml; /// using Microsoft.VisualStudio.TestTools.UnitTesting; /// using Z.ExtensionMethods.Object; /// /// namespace ExtensionMethods.Examples /// { /// [TestClass] /// public class System_Object_GetValueOrDefault /// { /// [TestMethod] /// public void GetValueOrDefault() /// { /// // Type /// var @this = new XmlDocument(); /// /// // Exemples /// string result1 = @this.GetValueOrDefault(x => x.FirstChild.InnerXml, "FizzBuzz"); // return "FizzBuzz"; /// string result2 = @this.GetValueOrDefault(x => x.FirstChild.InnerXml, () => "FizzBuzz"); // return "FizzBuzz" /// /// // Unit Test /// Assert.AreEqual("FizzBuzz", result1); /// Assert.AreEqual("FizzBuzz", result2); /// } /// } /// } /// </code> /// </example> public static TResult GetValueOrDefault<T, TResult>(this T @this, Func<T, TResult> func, Func<TResult> defaultValueFactory) { try { return func(@this); } catch (Exception) { return defaultValueFactory(); } } /// <summary> /// A T extension method that gets value or default. /// </summary> /// <typeparam name="T">Generic type parameter.</typeparam> /// <typeparam name="TResult">Type of the result.</typeparam> /// <param name="this">The @this to act on.</param> /// <param name="func">The function.</param> /// <param name="defaultValueFactory">The default value factory.</param> /// <returns>The value or default.</returns> /// <example> /// <code> /// using System.Xml; /// using Microsoft.VisualStudio.TestTools.UnitTesting; /// /// /// namespace ExtensionMethods.Examples /// { /// [TestClass] /// public class System_Object_GetValueOrDefault /// { /// [TestMethod] /// public void GetValueOrDefault() /// { /// // Type /// var @this = new XmlDocument(); /// /// // Exemples /// string result1 = @this.GetValueOrDefault(x => x.FirstChild.InnerXml, "FizzBuzz"); // return "FizzBuzz"; /// string result2 = @this.GetValueOrDefault(x => x.FirstChild.InnerXml, () => "FizzBuzz"); // return "FizzBuzz" /// /// // Unit Test /// Assert.AreEqual("FizzBuzz", result1); /// Assert.AreEqual("FizzBuzz", result2); /// } /// } /// } /// </code> /// </example> /// <example> /// <code> /// using System.Xml; /// using Microsoft.VisualStudio.TestTools.UnitTesting; /// using Z.ExtensionMethods.Object; /// /// namespace ExtensionMethods.Examples /// { /// [TestClass] /// public class System_Object_GetValueOrDefault /// { /// [TestMethod] /// public void GetValueOrDefault() /// { /// // Type /// var @this = new XmlDocument(); /// /// // Exemples /// string result1 = @this.GetValueOrDefault(x => x.FirstChild.InnerXml, "FizzBuzz"); // return "FizzBuzz"; /// string result2 = @this.GetValueOrDefault(x => x.FirstChild.InnerXml, () => "FizzBuzz"); // return "FizzBuzz" /// /// // Unit Test /// Assert.AreEqual("FizzBuzz", result1); /// Assert.AreEqual("FizzBuzz", result2); /// } /// } /// } /// </code> /// </example> /// <example> /// <code> /// using System.Xml; /// using Microsoft.VisualStudio.TestTools.UnitTesting; /// using Z.ExtensionMethods.Object; /// /// namespace ExtensionMethods.Examples /// { /// [TestClass] /// public class System_Object_GetValueOrDefault /// { /// [TestMethod] /// public void GetValueOrDefault() /// { /// // Type /// var @this = new XmlDocument(); /// /// // Exemples /// string result1 = @this.GetValueOrDefault(x => x.FirstChild.InnerXml, "FizzBuzz"); // return "FizzBuzz"; /// string result2 = @this.GetValueOrDefault(x => x.FirstChild.InnerXml, () => "FizzBuzz"); // return "FizzBuzz" /// /// // Unit Test /// Assert.AreEqual("FizzBuzz", result1); /// Assert.AreEqual("FizzBuzz", result2); /// } /// } /// } /// </code> /// </example> public static TResult GetValueOrDefault<T, TResult>(this T @this, Func<T, TResult> func, Func<T, TResult> defaultValueFactory) { try { return func(@this); } catch (Exception) { return defaultValueFactory(@this); } } }