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

Razor HtmlHelper Extensions (or other namespaces for views) Not Found

I don't know if this was happening in the PR or Beta, but if I create an extension method on HtmlHelper, it is not recognized in a Razor powered page: namespace SomeNamespace.Extensions { public static class HtmlExtensions { public…
swilliams
  • 48,060
  • 27
  • 100
  • 130
167
votes
1 answer

Can extension methods be applied to interfaces?

Is it possible to apply an extension method to an interface? (C# question) That is for example to achieve the following: create an ITopology interface create an extension method for this interface (e.g. public static int CountNodes(this ITopology…
Greg
  • 34,042
  • 79
  • 253
  • 454
155
votes
13 answers

Can I "multiply" a string (in C#)?

Suppose I have a string, for example, string snip = ""; I want to basically write it multiple times, depending on some integer value. string snip = ""; int multiplier = 2; // TODO: magic code to do this // snip * multiplier…
Ian G
  • 29,468
  • 21
  • 78
  • 92
146
votes
17 answers

Convert string to nullable type (int, double, etc...)

I am attempting to do some data conversion. Unfortunately, much of the data is in strings, where it should be int's or double, etc... So what I've got is something like: double? amount = Convert.ToDouble(strAmount); The problem with this approach…
Nathan Koop
  • 24,803
  • 25
  • 90
  • 125
141
votes
9 answers

AddRange to a Collection

A coworker asked me today how to add a range to a collection. He has a class that inherits from Collection. There's a get-only property of that type that already contains some items. He wants to add the items in another collection to the property…
TrueWill
  • 25,132
  • 10
  • 101
  • 150
122
votes
2 answers

How do I write an extension method in JavaScript?

I need to write a few extension methods in JS. I know just how to do this in C#. Example: public static string SayHi(this Object name) { return "Hi " + name + "!"; } and then called by: string firstName = "Bob"; string hi =…
Matt Cashatt
  • 23,490
  • 28
  • 78
  • 111
119
votes
3 answers

.NET List Concat vs AddRange

What is the difference between the AddRange and Concat functions on a generic List? Is one recommended over the other?
johnc
  • 39,385
  • 37
  • 101
  • 139
110
votes
4 answers

Is there any way in C# to override a class method with an extension method?

There have been occasions where I would want to override a method in a class with an extension method. Is there any way to do that in C#? For example: public static class StringExtension { public static int GetHashCode(this string inStr) { …
Phred Menyhert
  • 2,420
  • 4
  • 19
  • 19
106
votes
5 answers

How to use Active Support core extensions

I have Active Support 3.0.3 installed and Rails 3.0.3 with Ruby 1.8.7. When I try to use 1.week.ago I get NoMethodError: undefined method 'week' for 1:Fixnum from (irb):2 The other core extensions seem to work. I tried it on a friend's computer…
griotspeak
  • 13,022
  • 13
  • 43
  • 54
106
votes
9 answers

How do I extend a class with c# extension methods?

Can extension methods be applied to the class? For example, extend DateTime to include a Tomorrow() method that could be invoked like: DateTime.Tomorrow(); I know I can use static DateTime Tomorrow(this Datetime value) { //... } Or public static…
David Glenn
  • 24,412
  • 19
  • 74
  • 94
104
votes
6 answers

How do I use Moq to mock an extension method?

I am writing a test that depends on the results of an extension method but I don't want a future failure of that extension method to ever break this test. Mocking that result seemed the obvious choice but Moq doesn't seem to offer a way to override…
patridge
  • 26,385
  • 18
  • 89
  • 135
102
votes
2 answers

Where is the "Fold" LINQ Extension Method?

I found in MSDN's Linq samples a neat method called Fold() that I want to use. Their example: double[] doubles = { 1.7, 2.3, 1.9, 4.1, 2.9 }; double product = doubles.Fold((runningProduct, nextFactor) => runningProduct * nextFactor);…
Ken
  • 5,337
  • 3
  • 28
  • 19
97
votes
5 answers

How to extend C# built-in types, like String?

I need to Trim a String. But I want to remove all the repeated blank spaces within the String itself, not only at the end or at the start of it. I could do it with a method like: public static string ConvertWhitespacesToSingleSpaces(string value) { …
Girardi
  • 2,734
  • 3
  • 35
  • 50
90
votes
5 answers

Is it possible to create constructor-extension-method ? how?

Is it possible to add a constructor extension method? Sample Use Case I want to add a List< T > constructor to receive specific amount of bytes out of a given partially filled buffer (without the overhead of copying only the relevant bytes and so…
Tar
  • 8,529
  • 9
  • 56
  • 127
90
votes
6 answers

Why is the 'this' keyword required to call an extension method from within the extended class

I have created an extension method for an ASP.NET MVC ViewPage, e.g: public static class ViewExtensions { public static string Method(this ViewPage page) where T : class { return "something"; } } When calling this method…
M4N
  • 94,805
  • 45
  • 217
  • 260