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

Python extension methods

OK, in C# we have something like: public static string Destroy(this string s) { return ""; } So basically, when you have a string you can do: str = "This is my string to be destroyed"; newstr = str.Destroy() # instead of newstr =…
Shaokan
  • 7,438
  • 15
  • 56
  • 80
59
votes
16 answers

Error when using extension methods in C#

I came across an issue that makes me think there is bug in the 3.0 framework. When I try to use extension methods I get the following error: Missing compiler required member 'System.Runtime.CompilerServices.ExtensionAttribute..ctor' When using…
Korbin
  • 1,788
  • 2
  • 18
  • 29
58
votes
8 answers

Is calling an extension method on a "null" reference (i.e. event with no subscribers) evil?

Evil or not evil? public static void Raise(this EventHandler handler, object sender, EventArgs args) { if (handler != null) { handler(sender, args); } } // Usage: MyButtonClicked.Raise(this, EventArgs.Empty); // This works too!…
Judah Gabriel Himango
  • 58,906
  • 38
  • 158
  • 212
56
votes
8 answers

ASP.NET repeater alternate row highlighting without full blown

I'm trying to accomplish simply adding a css class to a div on alternate rows in my without going to the overhead of including a full blown which will force me to keep a lot of markup in sync in the…
Kieran Benton
  • 8,739
  • 12
  • 53
  • 77
56
votes
13 answers

What are Extension Methods?

What are extension methods in .NET? EDIT: I have posted a follow up question at Usage of Extension Methods
Developer
  • 17,809
  • 26
  • 66
  • 92
55
votes
4 answers

Calculating Count for IEnumerable (Non Generic)

Can anyone help me with a Count extension method for IEnumerable (non generic interface). I know it is not supported in LINQ but how to write it manually?
Homam
  • 23,263
  • 32
  • 111
  • 187
54
votes
6 answers

What is the easiest way to get the property value from a passed lambda expression in an extension method for HtmlHelper?

I am writing a dirty little extension method for HtmlHelper so that I can say something like HtmlHelper.WysiwygFor(lambda) and display the CKEditor. I have this working currently but it seems a bit more cumbersome than I would prefer. I am hoping…
Andrew Siemer
  • 10,166
  • 3
  • 41
  • 61
54
votes
7 answers

Enumeration extension methods

In vs2008, is it possible to write an extension methods which would apply to any enumeration. I know you can write extension methods against a specific enumeration, but I want to be able to every enumeration using a single extension method. Is this…
Eric Haskins
  • 8,505
  • 12
  • 38
  • 47
52
votes
2 answers

How to create an extension method in TypeScript for 'Date' data type

I have tried to create an extension method in TypeScript based on this discussion (https://github.com/Microsoft/TypeScript/issues/9), but I couldn't create a working one. Here is my code, namespace Mynamespace { interface Date { …
AhammadaliPK
  • 3,448
  • 4
  • 21
  • 39
51
votes
6 answers

Raising C# events with an extension method - is it bad?

We're all familiar with the horror that is C# event declaration. To ensure thread-safety, the standard is to write something like this: public event EventHandler SomethingHappened; protected virtual void OnSomethingHappened(EventArgs e) { …
Ryan Lundy
  • 204,559
  • 37
  • 180
  • 211
51
votes
9 answers

Extension methods versus inheritance

Are there rules of thumb that help determine which to use in what case? Should I prefer one over the other most times? Thanks!
djcouchycouch
  • 12,724
  • 13
  • 69
  • 108
50
votes
5 answers

Extension methods conflict

Lets say I have 2 extension methods to string, in 2 different namespaces: namespace test1 { public static class MyExtensions { public static int TestMethod(this String str) { return 1; } }…
Yochai Timmer
  • 48,127
  • 24
  • 147
  • 185
50
votes
4 answers

C# extension method as an interface implementation

I was wondering if a C# extension method of some class could act as an implementation of interface? What do I have: An iterface: public interface IEventHandler { void Notify(SEvent ev, IEventEmmiter source); } A class that implements it: class…
PJK
  • 2,082
  • 3
  • 17
  • 28
50
votes
2 answers

Does Array.ToArray<>() return the original array if it is the same type?

I deal with a framework on a daily basis where we sometimes provide methods that accept IEnumerable as a parameter in order to show user interfaces, perform calculations etc. If I pass in an array of MyBusinessObject like…
Codesleuth
  • 10,321
  • 8
  • 51
  • 71
49
votes
4 answers

Why doesn't the Controls collection provide all of the IEnumerable methods?

I'm not for sure how the ControlCollection of ASP.Net works, so maybe someone can shed some light on this for me. I recently discovered the magic that is extension methods and Linq. Well, I was very sad to find that this isn't valid syntax var…
Earlz
  • 62,085
  • 98
  • 303
  • 499