Language: C#
Null Extension Methods - Oh My!
The following extensions methods can help you in the situation below: public static TResult IfNotNull<TSource, TResult>(this TSource obj, Func<TSource, TResult> func) { return IfNotNull(obj, func, default(TResult)); } public static TResult IfNotNull<TSource, TResult>(this TSource obj, Func<TSource, TResult> func, TResult defaultIfNull) { return obj == null ? defaultIfNull : func(obj); } Instead of ... if (null != tree && null != tree.Headings && tree.Headings.Count > 0) { ... } you can do this ... if (tree.IfNotNull(t => t.Headings).IfNotNull(headings => headings.Count) > 0) { ... } ABPS: http://code.logos.com/blog/2008/01/nullpropagating_extension_meth.html
Report Abuse
Subscribe
Discuss
What's new
What is it
New Snippet
Recent Snippets
My Snippets
Web Code
Search

