snipt

Ctrl+h for KB shortcuts

C#

Generic extension method to filter out null elements in an array (useful for param arrays)

        /// <summary>
        /// Filter out null elements in an array of any type
        /// </summary>
        /// <typeparam name="T">Type of elements in the array</typeparam>
        /// <param name="source">Array instance to operate on</param>
        /// <returns></returns>
        public static T[] FilterOutNullElements<T>(this T[] source)
        {
            return source.Where<T>(item => item != null).ToArray<T>();
        }
https://snipt.net/embed/8b19224d998da550125a472629f3d67a/
/raw/8b19224d998da550125a472629f3d67a/
8b19224d998da550125a472629f3d67a
csharp
C#
11
2019-06-19T03:45:23
True
False
False
/api/public/snipt/2622/
generic-extension-method-to-filter-out-null-elements-in-an-array-useful-for-param-arrays
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1"> 1</a> <a href="#L-2"> 2</a> <a href="#L-3"> 3</a> <a href="#L-4"> 4</a> <a href="#L-5"> 5</a> <a href="#L-6"> 6</a> <a href="#L-7"> 7</a> <a href="#L-8"> 8</a> <a href="#L-9"> 9</a> <a href="#L-10">10</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a> <span class="c1">/// &lt;summary&gt;</span> </span><span id="L-2"><a name="L-2"></a> <span class="c1">/// Filter out null elements in an array of any type</span> </span><span id="L-3"><a name="L-3"></a> <span class="c1">/// &lt;/summary&gt;</span> </span><span id="L-4"><a name="L-4"></a> <span class="c1">/// &lt;typeparam name=&quot;T&quot;&gt;Type of elements in the array&lt;/typeparam&gt;</span> </span><span id="L-5"><a name="L-5"></a> <span class="c1">/// &lt;param name=&quot;source&quot;&gt;Array instance to operate on&lt;/param&gt;</span> </span><span id="L-6"><a name="L-6"></a> <span class="c1">/// &lt;returns&gt;&lt;/returns&gt;</span> </span><span id="L-7"><a name="L-7"></a> <span class="k">public</span> <span class="k">static</span> <span class="n">T</span><span class="p">[]</span> <span class="n">FilterOutNullElements</span><span class="p">&lt;</span><span class="n">T</span><span class="p">&gt;(</span><span class="k">this</span> <span class="n">T</span><span class="p">[]</span> <span class="n">source</span><span class="p">)</span> </span><span id="L-8"><a name="L-8"></a> <span class="p">{</span> </span><span id="L-9"><a name="L-9"></a> <span class="k">return</span> <span class="n">source</span><span class="p">.</span><span class="n">Where</span><span class="p">&lt;</span><span class="n">T</span><span class="p">&gt;(</span><span class="n">item</span> <span class="p">=&gt;</span> <span class="n">item</span> <span class="p">!=</span> <span class="k">null</span><span class="p">).</span><span class="n">ToArray</span><span class="p">&lt;</span><span class="n">T</span><span class="p">&gt;();</span> </span><span id="L-10"><a name="L-10"></a> <span class="p">}</span> </span></pre></div> </td></tr></table>
"extension method", "param arrays", c#, generic