c# - Question on two different class extension patterns -


what semantic difference between following 2 methods:

public static bool isnullorempty(this array value) {     return (value == null || value.length == 0); } 

and

public static bool isnullorempty<t>(this t[] value) {     return (value == null || value.length == 0); } 

is there advantage 1 on other?

the first work any array, including rectangular arrays , ones non-zero lower bound. work when compile-time type of array array, may happen weakly-typed apis.

in short, first more general, , should work anywhere second does.

(i'm assuming don't want "extra" features this, such constraints on t in second form... want find out whether array reference null or refers empty array.)

edit: ienumerable, you'd use:

public static bool isnullorempty(this ienumerable value) {     if (value == null)     {         return true;     }     var iterator = value.getenumerator();     try     {         return !iterator.movenext();     }         {         // non-generic ienumerator doesn't extend idisposable         idisposable disposable = iterator idisposable;         if (disposable != null)         {             disposable.dispose();         }     } } 

the disadvantage of of course can have side-effects - example, pass in linq query end talking database.


Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

visual c++ - Using relative values in array sorting ( asm ) -