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
88
votes
8 answers

Swift extension example

I was originally wanting to know how to make something like this UIColor.myCustomGreen so that I could define my own colors and use them throughout my app. I had studied extensions before and I thought that I could probably use them to solve my…
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
85
votes
30 answers

What Advantages of Extension Methods have you found?

A "non-believer" of C# was asking me what the purpose to extension methods was. I explained that you could then add new methods to objects that were already defined, especially when you don't own/control the source to the original object. He brought…
Jerry
  • 4,507
  • 9
  • 50
  • 79
82
votes
7 answers

Reflection to Identify Extension Methods

In C# is there a technique using reflection to determine if a method has been added to a class as an extension method? Given an extension method such as the one shown below is it possible to determine that Reverse() has been added to the string…
Mike Chess
  • 2,758
  • 5
  • 29
  • 37
81
votes
7 answers

Can C# extension methods access private variables?

Is it possible to access an object's private variables using an extension method?
Geo
  • 93,257
  • 117
  • 344
  • 520
79
votes
8 answers

Pass a lambda expression in place of IComparer or IEqualityComparer or any single-method interface?

I happened to have seen some code where this guy passed a lambda expression to a ArrayList.Sort(IComparer here) or a IEnumerable.SequenceEqual(IEnumerable list, IEqualityComparer here) where an IComparer or an IEqualityComparer was expected. I can't…
Water Cooler v2
  • 32,724
  • 54
  • 166
  • 336
78
votes
8 answers

How to compare nullable types?

I have a few places where I need to compare 2 (nullable) values, to see if they're the same. I think there should be something in the framework to support this, but can't find anything, so instead have the following: public static bool…
David_001
  • 5,703
  • 4
  • 29
  • 55
73
votes
40 answers

What is the best or most interesting use of Extension Methods you've seen?

I'm starting to really love extension methods... I was wondering if anyone her has stumbled upon one that really blew their mind, or just found clever. An example I wrote today: Edited due to other users' comments: public static IEnumerable
Mark Carpenter
  • 17,445
  • 22
  • 96
  • 149
73
votes
11 answers

Is it possible to implement mixins in C#?

I've heard that it's possible with extension methods, but I can't quite figure it out myself. I'd like to see a specific example if possible. Thanks!
Stewart Johnson
  • 14,281
  • 7
  • 61
  • 70
71
votes
43 answers

What's your favorite LINQ to Objects operator which is not built-in?

With extension methods, we can write handy LINQ operators which solve generic problems. I want to hear which methods or overloads you are missing in the System.Linq namespace and how you implemented them. Clean and elegant implementations, maybe…
Nappy
  • 3,016
  • 27
  • 39
67
votes
8 answers

Extension methods syntax vs query syntax

I'm trying to get a handle on if there's a good time to use standard linq keywords or linq extension methods with lambda expressions. They seems to do the same thing, just are written differently. Is it purely a matter of style? var query = from p…
Armstrongest
  • 15,181
  • 13
  • 67
  • 106
67
votes
7 answers

Can There Be Private Extension Methods?

Let's say I have a need for a simple private helper method, and intuitively in the code it would make sense as an extension method. Is there any way to encapsulate that helper to the only class that actually needs to use it? For example, I try…
David
  • 208,112
  • 36
  • 198
  • 279
64
votes
6 answers

Detect target framework version at compile time

I have some code which makes use of Extension Methods, but compiles under .NET 2.0 using the compiler in VS2008. To facilitate this, I had to declare ExtensionAttribute: /// /// ExtensionAttribute is required to define extension methods…
Matt Whitfield
  • 6,436
  • 3
  • 29
  • 44
64
votes
5 answers

Scala equivalent of C#’s extension methods?

In C# you can write: using System.Numerics; namespace ExtensionTest { public static class MyExtensions { public static BigInteger Square(this BigInteger n) { return n * n; } static void Main(string[] args) { BigInteger…
John
  • 641
  • 1
  • 6
  • 3
63
votes
14 answers

Extension Methods not Recognized

What is necessary to have an extension method honored when it exists in an imported assembly? I built one in a class library project but it is not recognized in my web project which references the library. All the other classes and methods in the…
ChiliYago
  • 11,341
  • 23
  • 79
  • 126
61
votes
6 answers

Extension methods in Python

Does Python have extension methods like C#? Is it possible to call a method like: MyRandomMethod() on existing types like int? myInt.MyRandomMethod()
Joan Venge
  • 315,713
  • 212
  • 479
  • 689