Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 845331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T06:25:57+00:00 2026-05-15T06:25:57+00:00

edit4: wikified, since this seems to have morphed more into a discussion than a

  • 0

edit4: wikified, since this seems to have morphed more into a discussion than a specific question.

In C++ programming, it’s generally considered good practice to "prefer non-member non-friend functions" instead of instance methods. This has been recommended by Scott Meyers in this classic Dr. Dobbs article, and repeated by Herb Sutter and Andrei Alexandrescu in C++ Coding Standards (item 44); the general argument being that if a function can do its job solely by relying on the public interface exposed by the class, it actually increases encapsulation to have it be external. While this confuses the "packaging" of the class to some extent, the benefits are generally considered worth it.

Now, ever since I’ve started programming in C#, I’ve had a feeling that here is the ultimate expression of the concept that they’re trying to achieve with "non-member, non-friend functions that are part of a class interface". C# adds two crucial components to the mix – the first being interfaces, and the second extension methods:

  • Interfaces allow a class to formally specify their public contract, the methods and properties that they’re exposing to the world.
  • Any other class can choose to implement the same interface and fulfill that same contract.
  • Extension methods can be defined on an interface, providing any functionality that can be implemented via the interface to all implementers automatically.
  • And best of all, because of the "instance syntax" sugar and IDE support, they can be called the same way as any other instance method, eliminating the cognitive overhead!

So you get the encapsulation benefits of "non-member, non-friend" functions with the convenience of members. Seems like the best of both worlds to me; the .NET library itself providing a shining example in LINQ. However, everywhere I look I see people warning against extension method overuse; even the MSDN page itself states:

In general, we recommend that you implement extension methods sparingly and only when you have to.

(edit: Even in the current .NET library, I can see places where it would’ve been useful to have extensions instead of instance methods – for example, all of the utility functions of List<T> (Sort, BinarySearch, FindIndex, etc.) would be incredibly useful if they were lifted up to IList<T> – getting free bonus functionality like that adds a lot more benefit to implementing the interface.)

So what’s the verdict? Are extension methods the acme of encapsulation and code reuse, or am I just deluding myself?

(edit2: In response to Tomas – while C# did start out with Java’s (overly, imo) OO mentality, it seems to be embracing more multi-paradigm programming with every new release; the main thrust of this question is whether using extension methods to drive a style change (towards more generic / functional C#) is useful or worthwhile..)

edit3: overridable extension methods

The only real problem identified so far with this approach, is that you can’t specialize extension methods if you need to. I’ve been thinking about the issue, and I think I’ve come up with a solution.
Suppose I have an interface MyInterface, which I want to extend –

I define my extension methods in a MyExtension static class, and pair it with another interface, call it MyExtensionOverrider. MyExtension methods are defined according to this pattern:

public static int MyMethod(this MyInterface obj, int arg, bool attemptCast=true)
{
    if (attemptCast && obj is MyExtensionOverrider)
    {
        return ((MyExtensionOverrider)obj).MyMethod(arg);
    }
    // regular implementation here
}

The override interface mirrors all of the methods defined in MyExtension, except without the this or attemptCast parameters:

public interface MyExtensionOverrider
{
    int MyMethod(int arg);
    string MyOtherMethod();
}

Now, any class can implement the interface and get the default extension functionality:

public class MyClass : MyInterface { ... }

Anyone that wants to override it with specific implementations can additionally implement the override interface:

public class MySpecializedClass : MyInterface, MyExtensionOverrider
{
    public int MyMethod(int arg) 
    { 
        //specialized implementation for one method
    }
    public string MyOtherMethod() 
    {   // fallback to default for others
        MyExtension.MyOtherMethod(this, attemptCast: false); 
    }
}

And there we go: extension methods provided on an interface, with the option of complete extensibility if needed. Fully general too, the interface itself doesn’t need to know about the extension / override, and multiple extension / override pairs can be implemented without interfering with each other.

I can see three problems with this approach –

  1. It’s a little bit fragile – the extension methods and override interface have to be kept synchronized manually.
  2. It’s a little bit ugly – implementing the override interface involves boilerplate for every function you don’t want to specialize.
  3. It’s a little bit slow – there’s an extra bool comparison and cast attempt added to the mainline of every method.

Still, all those notwithstanding, I think this is the best we can get until there’s language support for interface functions. Thoughts?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T06:25:58+00:00Added an answer on May 15, 2026 at 6:25 am

    I generally like extension methods, particularly on interfaces, but I have two issues with them:

    First, if an implementation has a more efficient way of achieving the extension method’s purpose, there’s no general way of expressing that. For example, Enumerable.Count() explicitly knows about ICollection/ICollection<T> and special-cases it. An alternative for this would be if interfaces could actually contain implementations directly, referencing only other interface methods and not declaring fields. The methods could then be overridden in appropriate implementations. This does mean you need to own the interface, of course… but in some cases it would be cleaner than current extension methods. (By avoiding the ability to introduce fields, I believe you get round some implementation problems which multiple inheritance of classes would introduce.)

    Second, I don’t like the way extension methods are discovered. There’s no way to say, “I want the extension methods from class X” without also dragging in the extension methods from other classes in the same namespace. I would like you to be able to write:

    using static System.Linq.Enumerable;
    

    to pick up only those extension methods.

    (Incidentally, I’ll be talking more about both of these points at NDC 2010 on Thursday. Hopefully the talk will be recorded.)

    The ability to specify general algorithms which only rely on the public interface is nice. The ability to call those algorithms on the type providing the interface is nice. The current mechanism just has a few sharp corners.

    Incidentally, it might be quite nice to be able to write methods within a type but say, “Limit me to only using the public API.”

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

EDIT: This question is more about language engineering than C++ itself. I used C++
--EDIT-- I believe this is a valid question that may have multiple answers (as
EDIT . Since this question was asked, I got a PhD on solving linear
EDIT 07/14 As Bill Burgess mentionned in a comment of his answer, this question
Edit (updated question) I have a simple C program: // it is not important
Edit 4: A new formatting for the question BACKGROUND: I have a class, Window,
(EDIT: I have asked the wrong question. The real problem I'm having is over
I am working on a project involving Dynamic Programming and am struck on this
Edit4: I'm happy working in this in Linux now, as I got the rest
edit: change of question if my code is like this: <form name=login action=https://login.extremebb.net/login method=post

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.