Questions tagged [extension-methods]

An extension method is a language feature of some languages, such as Swift, Visual Basic.NET and C#. Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type.

An Extension method is a new language feature of C# starting with the 3.0 specification, as well as Visual Basic.NET starting with 9.0 and Oxygene with 2.0, and is a fundamental aspect of Swift. Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are a special kind of static method, but they are called as if they were instance methods on the extended type. For client code written in C# and Visual Basic, there is no apparent difference between calling an extension method and the methods that are actually defined in a type.

3545 questions
33
votes
3 answers

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

In the course of my maintenance for an older application that badly violated the cross-thread update rules in winforms, I created the following extension method as a way to quickly fix illegal calls when I've discovered them: /// ///…
Greg D
  • 43,259
  • 14
  • 84
  • 117
33
votes
2 answers

Extension method for List AddToFront(T object) how to?

I want to write an extension method for the List class that takes an object and adds it to the front instead of the back. Extension methods really confuse me. Can someone help me out with this? myList.AddToFront(T object);
EpiX
  • 1,281
  • 2
  • 16
  • 22
33
votes
6 answers

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

Here's how I would add one item to an IEnumerable object: //Some IEnumerable object IEnumerable arr = new string[] { "ABC", "DEF", "GHI" }; //Add one item arr = arr.Concat(new string[] { "JKL" }); This is awkward. I don't see a method…
user2023861
  • 8,030
  • 9
  • 57
  • 86
33
votes
4 answers

Repository Methods vs. Extending IQueryable

I have repositories (e.g. ContactRepository, UserRepository and so forth) which encapsulate data access to the domain model. When I was looking at searching for data, e.g. finding a contact whose first name starts with XYZ a contact whose…
Alex
  • 75,813
  • 86
  • 255
  • 348
32
votes
4 answers

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

I have a set of extension methods that I regularly use for various UI tasks. I typically define them to run off of type object, even though inside of them I'm typically converting them to string types. public static string FormatSomething(this…
Doozer Blake
  • 7,677
  • 2
  • 29
  • 40
32
votes
5 answers

Why IReadOnlyCollection has ElementAt but not IndexOf

I am working with a IReadOnlyCollection of objects. Now I'm a bit surprised, because I can use linq extension method ElementAt(). But I don't have access to IndexOf(). This to me looks a bit illogical: I can get the element at a given position, but…
Lorenzo Santoro
  • 464
  • 1
  • 6
  • 16
32
votes
1 answer

Using extension methods in .NET 2.0?

I want to do this, but getting this error: Error 1 Cannot define a new extension method because the compiler required type 'System.Runtime.CompilerServices.ExtensionAttribute' cannot be found. Are you missing a reference to System.Core.dll? …
Joan Venge
  • 315,713
  • 212
  • 479
  • 689
32
votes
2 answers

Extension methods defined on value types cannot be used to create delegates - Why not?

Extension methods can be assigned to delegates that match their usage on an object, like this: static class FunnyExtension { public static string Double(this string str) { return str + str; } public static int Double(this int num) { return…
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
31
votes
3 answers

Interface + Extension (mixin) vs Base Class

Is an interface + extension methods (mixin) preferable to an abstract class? If your answer is "it depends", what does it depend upon? I see two possible advantages to the interface + extension approach. Interfaces are multiply inheritable and…
dss539
  • 6,804
  • 2
  • 34
  • 64
31
votes
4 answers

Where do I put my extension method?

A senior member here gave me this code: public static string Truncate(this string value, int maxChars) { return value.Length <= maxChars ? value : value.Substring(0, maxChars) + " .."; } He said to use it as an extension method. But where do I…
Melony
  • 1,341
  • 3
  • 10
  • 6
31
votes
1 answer

Static extension methods on Seq module

According to this post, F# supports extension methods on object instances and static classes. For example: module CollectionExtensions = type System.Linq.Enumerable with static member RangeChar(first:char, last:char) = {first ..…
Juliet
  • 80,494
  • 45
  • 196
  • 228
31
votes
10 answers

Resolving extension methods/LINQ ambiguity

I'm writing an add-in for ReSharper 4. For this, I needed to reference several of ReSharper's assemblies. One of the assemblies (JetBrains.Platform.ReSharper.Util.dll) contains a System.Linq namespace, with a subset of extension methods already…
Igal Tabachnik
  • 31,174
  • 15
  • 92
  • 157
31
votes
3 answers

Anonymous Types - Are there any distingushing characteristics?

Is there anything to use, to determine if a type is actually a anonymous type? For example an interface, etc? The goal is to create something like the following... //defined like... public static T Get(this IAnonymous obj, string prop) { …
hugoware
  • 35,731
  • 24
  • 60
  • 70
31
votes
7 answers

Disadvantages of extension methods?

Extension method is a really helpful feature that you can add a lot of functions you want in any class. But I am wondering if there is any disadvantage that might bring troubles to me. Any comments or suggestions?
Sonic Lee
  • 1,411
  • 2
  • 14
  • 15
30
votes
1 answer

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

I read in a comment on a similar question for C# that extension methods can only be called from instances. Is the same true for Dart? I'm trying to add a getter to the Platform class that will be called like so. Platform.isDesktop. However, this…
ThinkDigital
  • 3,189
  • 4
  • 26
  • 34