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
937
votes
6 answers

Does C# have extension properties?

Does C# have extension properties? For example, can I add an extension property to DateTimeFormatInfo called ShortDateLongTimeFormat which would return ShortDatePattern + " " + LongTimePattern?
Svish
  • 152,914
  • 173
  • 462
  • 620
833
votes
20 answers

Distinct() with lambda?

Right, so I have an enumerable and wish to get distinct values from it. Using System.Linq, there's, of course, an extension method called Distinct. In the simple case, it can be used with no parameters, like: var distinctValues =…
Tor Haugen
  • 19,509
  • 9
  • 45
  • 63
709
votes
11 answers

Which method performs better: .Any() vs .Count() > 0?

in the System.Linq namespace, we can now extend our IEnumerable's to have the Any() and Count() extension methods. I was told recently that if i want to check that a collection contains 1 or more items inside it, I should use the .Any() extension…
Pure.Krome
  • 84,693
  • 113
  • 396
  • 647
630
votes
18 answers

Can I add extension methods to an existing static class?

I'm a fan of extension methods in C#, but haven't had any success adding an extension method to a static class, such as Console. For example, if I want to add an extension to Console, called 'WriteBlueLine', so that I can…
Leon Bambrick
  • 26,009
  • 9
  • 51
  • 75
478
votes
150 answers

What are your favorite extension methods for C#? (codeplex.com/extensionoverflow)

Let's make a list of answers where you post your excellent and favorite extension methods. The requirement is that the full code must be posted and a example and an explanation on how to use it. Based on the high interest in this topic I have setup…
bovium
  • 2,869
  • 6
  • 25
  • 35
471
votes
21 answers

Why is there no ForEach extension method on IEnumerable?

Inspired by another question asking about the missing Zip function: Why is there no ForEach extension method on the IEnumerable interface? Or anywhere? The only class that gets a ForEach method is List<>. Is there a reason why it's missing, maybe…
Cameron MacFarland
  • 70,676
  • 20
  • 104
  • 133
396
votes
9 answers

In C#, what happens when you call an extension method on a null object?

Does the method get called with a null value or does it give a null reference exception? MyObject myObject = null; myObject.MyExtensionMethod(); // <-- is this a null reference exception? If this is the case I will never need to check my 'this'…
tpower
  • 56,100
  • 19
  • 68
  • 100
324
votes
6 answers

Convert string[] to int[] in one line of code using LINQ

I have an array of integers in string form: var arr = new string[] { "1", "2", "3", "4" }; I need to an array of 'real' integers to push it further: void Foo(int[] arr) { .. } I tried to cast int and it of course…
abatishchev
  • 98,240
  • 88
  • 296
  • 433
276
votes
11 answers

Extension methods must be defined in a non-generic static class

I'm getting the error: Extension methods must be defined in a non-generic static class On the line: public class LinqHelper Here is the helper class, based on Mark Gavells code. I'm really confused as to what this error means as I am sure it was…
Tom Gullen
  • 61,249
  • 84
  • 283
  • 456
262
votes
8 answers

Mocking Extension Methods with Moq

I have a pre-existing Interface... public interface ISomeInterface { void SomeMethod(); } and I've extended this interface using a mixin... public static class SomeInterfaceExtensions { public static void AnotherMethod(this ISomeInterface…
Russell Giddings
  • 8,731
  • 5
  • 34
  • 35
218
votes
14 answers

Java equivalent to C# extension methods

I am looking to implement a functionality in a list of object as I would in C# using an extension method. Something like this: List list; // ... List initialization. list.getData(id); How do I do that in Java?
Fabio Milheiro
  • 8,100
  • 17
  • 57
  • 96
214
votes
4 answers

Code equivalent to the 'let' keyword in chained LINQ extension method calls

Using the C# compilers query comprehension features, you can write code like: var names = new string[] { "Dog", "Cat", "Giraffe", "Monkey", "Tortoise" }; var result = from animalName in names let nameLength = animalName.Length where…
LBushkin
  • 129,300
  • 32
  • 216
  • 265
200
votes
3 answers

How to add new methods to an existing type in Go?

I want to add a convenience util method on to gorilla/mux Route and Router types: package util import( "net/http" "github.com/0xor1/gorillaseed/src/server/lib/mux" ) func (r *mux.Route) Subroute(tpl string, h http.Handler) *mux.Route{ …
Daniel Robinson
  • 13,806
  • 18
  • 64
  • 112
191
votes
7 answers

Operator Overloading with C# Extension Methods

I'm attempting to use extension methods to add an operater overload to the C# StringBuilder class. Specifically, given StringBuilder sb, I'd like sb += "text" to become equivalent to sb.Append("text"). Here's the syntax for creating an extension…
Jude Allred
  • 10,977
  • 7
  • 28
  • 27
184
votes
3 answers

Static extension methods

Is there any way I can add a static extension method to a class. specifically I want to overload Boolean.Parse to allow an int argument.
Midhat
  • 17,454
  • 22
  • 87
  • 114
1
2 3
99 100