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
40
votes
5 answers

What is the performance of the Last() extension method for List?

I really like Last() and would use it all the time for Lists. But since it seems to be defined for IEnumerable, I guess it enumerates the enumeration first - this should be O(n) as opposed to O(1) for directly indexing the last element of a…
Daren Thomas
  • 67,947
  • 40
  • 154
  • 200
39
votes
6 answers

C# Extension Method for Object

Is it a good idea to use an extension method on the Object class? I was wondering if by registering this method if you were incurring a performance penalty as it would be loaded on every object that was loaded in the context.
Mandrake
  • 1,161
  • 1
  • 10
  • 11
39
votes
1 answer

Swift: Is it possible to add a protocol extension to a protocol?

Lets say I have two protocols: protocol TheirPcol {} protocol MyPcol { func extraFunc() } What I want to do is to create a protocol extension for 'TheirPcol' which lets extraFunc() work on anything which conforms to 'TheirPcol'. So something…
drekka
  • 20,957
  • 14
  • 79
  • 135
39
votes
6 answers

VB.NET: impossible to use Extension method on System.Object instance

Can I make an Extension method for all the subclasses of System.Object (everything)? Example: Public Function MyExtension(value As Object) As Object Return value End Function The above functions won't work for object instance: Dim…
Shimmy Weitzhandler
  • 101,809
  • 122
  • 424
  • 632
38
votes
3 answers

How to use Notification.Name extension from Swift to Objective-C?

I created an extension for Notification.Name as below: public extension Notification.Name { public static let blahblahblah = Notification.Name(rawValue: "blahblahblah") } Now I want to use this extension in Objective-C, but it's not accessible…
BARS
  • 629
  • 1
  • 6
  • 18
37
votes
7 answers

Why doesn't this generic extension method compile?

The code is a little weird, so bear with me (keep in mind this scenario did come up in production code). Say I've got this interface structure: public interface IBase { } public interface IChild : IBase { } public interface IFoo where T :…
Cameron
  • 96,106
  • 25
  • 196
  • 225
37
votes
2 answers

How do I use an extension method in an ASP.NET MVC View?

How do I access an extension method in an ASP.Net MVC View? In C# I do using MyProject.Extensions; and I remember seeing an XML equivalent to put in a view, but I can't find it anymore.
Pablo Fernandez
  • 279,434
  • 135
  • 377
  • 622
36
votes
4 answers

How to implement left join in JOIN Extension method

I am trying to implement an outer join on this kind of query for the p.Person table. How would I do this? This example is taken from http://ashishware.com/DSLinqExample.shtml var onlyinfo = p.Person .Where(n => n.FirstName.Contains('a')) …
Shantanu Gupta
  • 20,688
  • 54
  • 182
  • 286
35
votes
9 answers

If condition in LINQ Where clause

With Linq, can I use a conditional statement inside of a Where extension method?
35
votes
3 answers

How can I get an extension method to change the original object?

I want to be able to write extension methods so that I can say: lines.ForceSpaceGroupsToBeTabs(); instead of: lines = lines.ForceSpaceGroupsToBeTabs(); However, the following code currently outputs: ....one ........two instead…
Edward Tanguay
  • 189,012
  • 314
  • 712
  • 1,047
35
votes
2 answers

Is it possible to write extension methods for Console?

While looking at this question and it's answers I thought that it would be a good idea to write an extension method for System.Console that contained the desired functionality. However, when I tried it, I got this compiler error System.Console':…
CoderDennis
  • 13,642
  • 9
  • 69
  • 105
35
votes
8 answers

Evil use of Maybe monad and extension methods in C#?

edit 2015 This question and its answers are no longer relevant. It was asked before the advent of C# 6, which has the null propagating opertor (?.), which obviates the hacky-workarounds discussed in this question and subsequent answers. As of 2015,…
Judah Gabriel Himango
  • 58,906
  • 38
  • 158
  • 212
34
votes
3 answers

Define an Extension Method for IEnumerable which returns IEnumerable?

How do I define an Extension Method for IEnumerable which returns IEnumerable? The goal is to make the Extension Method available for all IEnumerable and IEnumerable where T can be an anonymous type.
SharePoint Newbie
  • 5,974
  • 12
  • 62
  • 103
34
votes
2 answers

How to use unsafe code in safe contex?

I need to use SecureString for a Microsoft's class and i found the following code on the internet: public static class SecureStringExt { public static SecureString ConvertToSecureString(this string password) { if (password == null) …
CodeArtist
  • 5,534
  • 8
  • 40
  • 65
34
votes
2 answers

What causes "extension methods cannot be dynamically dispatched" here?

Compile Error 'System.Data.SqlClient.SqlConnection' has no applicable method named 'Query' but appears to have an extension method by that name. Extension methods cannot be dynamically dispatched. Consider casting the dynamic arguments or calling…
Mike Perrenoud
  • 66,820
  • 29
  • 157
  • 232