Is it appropriate to extend Control to provide consistently safe Invoke/BeginInvoke functionality?

You should create Begin and End extension methods as well. And if you use generics, you can make the call look a little nicer. public static class ControlExtensions { public static void InvokeEx<T>(this T @this, Action<T> action) where T : Control { if (@this.InvokeRequired) { @this.Invoke(action, new object[] { @this }); } else { if … Read more

What’s the Best Way to Add One Item to an IEnumerable?

Nope, that’s about as concise as you’ll get using built-in language/framework features. You could always create an extension method if you prefer: arr = arr.Append(“JKL”); // or arr = arr.Append(“123”, “456”); // or arr = arr.Append(“MNO”, “PQR”, “STU”, “VWY”, “etc”, “…”); // … public static class EnumerableExtensions { public static IEnumerable<T> Append<T>( this IEnumerable<T> source, … Read more

What causes “extension methods cannot be dynamically dispatched” here?

So, can somebody please help me understand why leveraging the same overload inside of those other methods is failing with that error? Precisely because you’re using a dynamic value (param) as one of the arguments. That means it will use dynamic dispatch… but dynamic dispatch isn’t supported for extension methods. The solution is simple though: … Read more

Define an Extension Method for IEnumerable which returns IEnumerable?

The easiest way to write any iterator is with an iterator block, for example: static IEnumerable<T> Where<T>(this IEnumerable<T> data, Func<T, bool> predicate) { foreach(T value in data) { if(predicate(value)) yield return value; } } The key here is the “yield return“, which turns the method into an iterator block, with the compiler generating an enumerator … Read more

Is there a way to add a static extension method on a class directly in Dart?

Dart doesn’t support static extension methods. An extension method acts like an instance method, not like a static method. There is currently no way to add static methods to a class from outside the class. You can declare static methods in an extension, but they are static on the extension itself (you call them using … Read more

Why IReadOnlyCollection has ElementAt but not IndexOf

IReadOnlyCollection is a collection, not a list, so strictly speaking, it should not even have ElementAt(). This method is defined in IEnumerable as a convenience, and IReadOnlyCollection has it because it inherits it from IEnumerable. If you look at the source code, it checks whether the IEnumerable is in fact an IList, and if so … Read more

Is there a performance hit for creating Extension methods that operate off the type ‘object’?

Is there a performance hit for creating extension methods that operate off the object type? Yes. If you pass a value type in then the value type will be boxed. That creates a performance penalty of allocating the box and doing the copy, plus of course later having to garbage collect the box. Instead of … Read more