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
49
votes
4 answers

Is there any method like ForEach for IList?

Possible Duplicate: LINQ equivalent of foreach for IEnumerable List has a method called ForEach which executes the passed action on each element of it. var names = new List{ "Bruce", "Alfred", "Tim", "Richard" }; names.ForEach(p…
Joe.wang
  • 11,537
  • 25
  • 103
  • 180
48
votes
3 answers

Ambiguous call between two C# extension generic methods one where T:class and other where T:struct

Consider two extension methods: public static T MyExtension(this T o) where T:class public static T MyExtension(this T o) where T:struct And a class: class MyClass() { ... } Now call the extension method on a instance of the above class: var…
Lee Atkinson
  • 2,171
  • 3
  • 20
  • 32
48
votes
6 answers

C# naming convention for extension methods for interface

I typically name my C# interfaces as IThing. I'm creating an extension method class for IThing, but I don't know what to name it. On one hand, calling it ThingExtensions seems to imply it is an extension class to some Thing class instead of to the…
Sarah Vessels
  • 30,930
  • 33
  • 155
  • 222
47
votes
7 answers

Extension method on enumeration, not instance of enumeration

I have an enumeration for my Things like so: public enum Things { OneThing, AnotherThing } I would like to write an extension method for this enumeration (similar to Prise's answer here) but while that method works on an instance of the…
Jamezor
  • 2,488
  • 3
  • 23
  • 16
45
votes
9 answers

Partial Class vs Extension Method

I dont have much experience of using these 2 ways to extend a class or create extension methods against a class. By looking others work, I have a question here. I saw people using a partial class to extend an entity class in a project. Meanwhile, in…
ValidfroM
  • 2,626
  • 3
  • 30
  • 41
45
votes
6 answers

ArgumentNullException or NullReferenceException from extension method?

What would you consider to be the best exception type to throw when an extension method is called on a null instance (where the extension method does not allow it)? Since extension methods are nothing but static methods you could argue that it…
Patrik Hägne
  • 16,751
  • 5
  • 52
  • 60
44
votes
3 answers

Extension Methods vs Static Utility Class

I'm looking for some pros and cons for using extension methods over static utility classes in a C# app. For instance, a plus in the extension methods column is the convinience of calling by the class name rather than something like "StringUtils".…
AJM
  • 32,054
  • 48
  • 155
  • 243
44
votes
4 answers

Will the dynamic keyword in C#4 support extension methods?

I'm listening to a talk about C#4's dynamic keyword and I'm wondering... Will this feature be orthogonal to other .NET features, for example will it support extension methods? public static class StrExtension { public static string twice(this…
Motti
  • 110,860
  • 49
  • 189
  • 262
44
votes
10 answers

Best practices: C# Extension methods namespace and promoting extension methods

I know there exists already a post, describing nearly the same, but I think mine is a bit different. What I would like to know is how you organize your extension methods in terms of assigning the namespace. Currently - for the extension methods in…
Juri
  • 32,424
  • 20
  • 102
  • 136
42
votes
6 answers

Why use TagBuilder instead of StringBuilder?

what's the difference in using tag builder and string builder to create a table in a htmlhelper class, or using the HtmlTable? aren't they generating the same thing??
DaveDev
  • 41,155
  • 72
  • 223
  • 385
41
votes
4 answers

Extension methods on a struct

Can you add extension methods to a struct?
Ghyath Serhal
  • 7,466
  • 6
  • 44
  • 60
41
votes
8 answers

The operation cannot be completed because the DbContext has been disposed error

I'm new to EF and I'm trying to use an extension method which converts from my Database type User to my info class UserInfo. I'm using database first if that makes a difference? My code below gives the error The operation cannot be completed…
Colin
  • 2,442
  • 5
  • 24
  • 30
40
votes
4 answers

F# extension methods in C#

If you were to define some extension methods, properties in an assembly written in F#, and then use that assembly in C#, would you see the defined extensions in C#? If so, that would be so cool.
Joan Venge
  • 315,713
  • 212
  • 479
  • 689
40
votes
6 answers

Impossible to use ref and out for first ("this") parameter in Extension methods?

Why is it forbidden to call Extension Method with ref modifier? This one is possible: public static void Change(ref TestClass testClass, TestClass testClass2) { testClass = testClass2; } And this one not: public static void…
Hun1Ahpu
  • 3,315
  • 3
  • 28
  • 34
40
votes
5 answers

FindAll vs Where extension-method

I just want know if a "FindAll" will be faster than a "Where" extentionMethod and why? Example : myList.FindAll(item=> item.category == 5); or myList.Where(item=> item.category == 5); Which is better ?
Cédric Boivin
  • 10,854
  • 13
  • 57
  • 98