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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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:
zumalifeguard's user avatar
0 votes
1 answer

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

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

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

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

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

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