Questions tagged [extension-method]

22 questions
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 throw it manually. For the implementation of the…
thisextendsthat
  • 535
  • 1
  • 5
  • 11
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 callbacks inside class extension: extension…
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 that made wonder why are getting all these things…
edalorzo
  • 2,634
  • 1
  • 19
  • 28
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 / 1000.0; } public static double…
Moop
  • 305
  • 2
  • 6
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 { /// /// Shortcut for string.Format. ///…
P. Roe
  • 593
  • 3
  • 12
6
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 The coolest thing about it IMHO is that adding a…
Gulshan
  • 9,402
  • 10
  • 58
  • 89
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 speed relative to using double. But of course…
Conrad
  • 288
  • 2
  • 8
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.ID) has become : orders.ShippedOrderIds Extension…
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 more selective when using Extension methods but…
Bryan Harrington
  • 2,502
  • 1
  • 19
  • 22
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 that. Let's declare a simple interface for a…
Timitry
  • 161
  • 1
  • 6
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: http://www.drdobbs.com/cpp/how-non-member-functions-improve-encapsu/184401197 If you subscribe to his theory,…
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 methods are supposed to be used, it is not in the…
J86
  • 297
  • 2
  • 8
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 end, I wanted to create a transpose() extension…
Tara
  • 151
  • 3
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 to do that in swift. Here is my shot at…
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 all functions wrapped at this time, but I want to…
lucasbrendel
  • 131
  • 7
1
2