Skip to main content
OverflowAI is here! AI power for your Stack Overflow for Teams knowledge community. Learn more

Questions tagged [extension-method]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
1 vote
2 answers
196 views

Wrapping Business Logic In Extension Methods For POCOs

I have recently stumbled upon a use case of extension methods and wanted to gather some design thoughts. The problem: A IEnumerable of a POCO and within that list there should exist a POCO that ...
Stevenfowler16's user avatar
6 votes
4 answers
4k views

Which is preferred: subclass double or create extension methods to test (relative) equality due to floating point differences?

I am writing numerical calculation software using .NET C#, which needs to be blazingly fast. There is a lot of fractional math. So using decimal type is pretty much out of the question, given its poor ...
Rory's user avatar
  • 288
2 votes
0 answers
67 views

What is the idiomatic Swift way to add general functionality via protocol/extension?

I am a scala developer new to swift. In scala we can share implementation across a variety of classes by "extending" a trait that has default implementations for the methods. I would like to see how ...
WestCoastProjects's user avatar
-3 votes
1 answer
220 views

Receiver parameters in Go

I have no idea where to post this question since it isn't about the Go language specification itself but rather the justification. In the Go language, functions can have receiver parameters to ...
Niklas Vest's user avatar
4 votes
1 answer
1k views

Why is it bad to map between a Model and a ViewModel in an extension method

In a recent Pull Request (PR) of mine, a colleague suggested that it was a bad idea to map between a model and its view model via extension methods. I asked why and he said: It isn't how extension ...
J86's user avatar
  • 297
3 votes
1 answer
1k views

Alternative to a utility class for extension methods in C#?

I understand that in OOP languages like C#, it is generally viewed as bad practice to create utility classes, and it's preferable to put methods in the classes in which they will be used. To that ...
Tara's user avatar
  • 151
6 votes
4 answers
2k views

Replacing Linq Methods with Extension Methods

So, I've fallen into the fad trap, and started replacing a large amount of linq queries with extension methods. For example: orders.Where(o => o.Status == ShippedStatus.Shipped).Select(o => o....
JohnsonCore's user avatar
6 votes
4 answers
6k views

Extension methods vs. Static Class Methods

I've taken on a Visual Studio C# project where my previous colleague used a lot of Extension Methods in static classes over multiple files depending on the uses. In my previous experience I would be ...
Nayrb's user avatar
  • 2,504
0 votes
3 answers
158 views

Should I reuse a predicate for multiple IEnumerable checks

In a long running Windows Service I have a custom thread pool manager that has a loop which continuously looks through a couple lists for idle threads. Given multiple uses of the same predicate, is ...
rediVider's user avatar
  • 111
43 votes
13 answers
28k views

Is it unreasonable to expect Any() *not* to throw a null reference exception?

When you create an extension method you can, of course, call it on null.But, unlike an instance method call, calling it on null doesn't have to throw a NullReferenceException -> you have to check and ...
thisextendsthat's user avatar
0 votes
3 answers
721 views

When it isn't right to add types to a well known namespace?

For the purpose of writing a library, I found to be nice to add types to some well known namespace. Example: I've written a couple of extension methods for BinaryWriter and naturally put them in ...
aybe's user avatar
  • 765
5 votes
2 answers
3k views

Interface extension method returning the interface type in C#

Programming against interfaces is an often-heard good practice in software development. Together with extension methods, this provides a great functionality. However, in C#, there are limitations to ...
Timitry's user avatar
  • 161
2 votes
1 answer
1k views

Design Pattern for Library Wrapping Extern Methods

I am working to create a C# library that wraps a C DLL to integrate with our test system. The C DLL has probably close to 100 functions that can be accessed and all from the same DLL. I don't need ...
lucasbrendel's user avatar
13 votes
3 answers
6k views

For what reasons would you use a separate class extension for each delegate in Swift?

I was working through a Ray Wenderlich tutorial and noticed that the author uses class extensions to hold delegate callbacks rather than having them be handled in the class itself i.e.: delegate ...
morbidhawk's user avatar
1 vote
1 answer
329 views

Does merely parameterizing a dependency meet the requirements for Inversion of Control?

When I find a concrete dependency inside an extension method, I have been attempting to remove the (concrete) dependency by parameterising it like so // original implementation public static List<...
jhsowter's user avatar
  • 491
5 votes
1 answer
2k views

How can I use the "Non-Member Functions Improve Encapsulation" pattern from C#?

In 2000, Scott Meyers argued that non-member functions improve encapsulation. In C++, a non-member function is a C-style global function: http://www.drdobbs.com/cpp/how-non-member-functions-improve-...
zumalifeguard's user avatar
0 votes
1 answer
762 views

Is brevity in writing code beneficial when it requires using language constructs in a strange way [duplicate]

In C#, with extension methods, you can replace this line of code: TimeSpan interval = TimeSpan.FromMinutes(4); with this one: TimeSpan interval = 4.Minutes(); Extension method being: public ...
Kornelije Petak's user avatar
8 votes
4 answers
10k views

Should I put extension methods of an interface in the interface.cs file?

Imagine this set up: public interface IMass{ double Mass {get;} } public static class IMassExtension { public static double ToKg(this IMass massObject) { return massObject.Mass / ...
Moop's user avatar
  • 305
2 votes
3 answers
2k views

C# vector class - Interpolation design decision

Currently I'm coding a Vector class in C# and I'm coming to the point, where I've to figure out, how I want to implement a function/method to interpolate between two vectors. The (obvious) and also my ...
user avatar
11 votes
4 answers
1k views

What is Java's primary focus? Why does it take so long to get new features?

I have been exploring the new features in the JDK8, like the lambda expressions, the extension methods, and the new stream API. Evidently none of these features are new in the programming world and ...
edalorzo's user avatar
  • 2,656
6 votes
4 answers
6k views

C# String.format extension method

With the addtion of Extension methods to C# we've seen a lot of them crop up in our group. One debate revolves around extension methods like this one: public static class StringExt { /// <...
P. Roe's user avatar
  • 593
7 votes
2 answers
5k views

Why aren't extension methods being used more extensively in .NET BCL?

I found the "Extension method" concept of C# just wonderful. It enables us to do things like: More flexible alternative to Abstract Classes. Provide missing Java-like functionalities to C# enum. And ...
Gulshan's user avatar
  • 9,442