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 6996637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:10:59+00:00 2026-05-27T20:10:59+00:00

Imagine i have an extension method that operates of an interface of abstract objects:

  • 0

Imagine i have an extension method that operates of an interface of abstract objects:

public static void BlowTheirWhoHoovers(this ICollection<Image> source)
{
   ...
}

This extension knows how to blow the who hoovers of a list of images.

Note:

  • it’s a list of Images, which is abstract. As long as the lists contain anything that descends from Image (e.g. Bitmap, Metafile, PngImage) the extension can handle it
  • source list can by any kind of list as long as it exposes ICollection
  • since source list is going to potentially have items added or removed, it is ICollection rather than IEnumeration (IEnumeration doesn’t support modifying the list)

That’s all well and good, except it doesn’t work, the extension method is not found:

Collection<Bitmap> whoHonkers = new Collection<Bitmap>();
whoHonkers.BlowTheirWhoHoovers();

enter image description here

i assume this fails because:

  • Visual Studio doesn’t think Collection implements ICollection<T>
  • Visual Studio doesn’t think Bitmap descends from Image

Now i can change my extension:

public static void BlowTheirWhoHoovers(this Collection<Bitmap> source)
{
   ...
}

which does compile:

enter image description here

except it’s no longer useful (it requires Collection, and it requires Bitmap), e.g. it fails to work on anything except Collection<T>:

List<Bitmap> whoHonkers = new List<Bitmap>();
whoHonkers.BlowTheirWhoHoovers();

It also fails on anything that it’s Bitmap:

List<Metafile> whoHonkers = new List<Metafile>();
whoHonkers.BlowTheirWhoHoovers();

or

List<PngImage> whoHonkers = new List<PngImage>();
whoHonkers.BlowTheirWhoHoovers();

How can i apply an extension to anything that is:

ICollection<Image>

?

i can’t force anyone to change their declaration, e.g.:

List<Bitmap>

to

Collection<Image>

Update:

Possible avenues of solution exploration:

  • finding the syntax that allows extensions on things that the compiler should be able to do
  • manually casting the list; doing the compiler’s job for it

Update#2:

i could have simplified some of the distractions around ICollection, and made it IEnumerable (since it’s a hypothetical example i can make up whatever i want). But the screenshots already show Collection, so i’m going to leave it as it is.

Update#3:

As foson pointed out, changing it to IEnumerable allows a way to cheat the answer. So it definitely should stay as ICollection.

  • 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-27T20:11:00+00:00Added an answer on May 27, 2026 at 8:11 pm

    Sounds like a covarient problem.

    If you change the signature to

    public static void BlowTheirWhoHoovers(this IEnumerable<Image> source)

    you should be able to do it in .NET 4 (not SL).

    The problem with varience is that if you passed into the method an ICollection<JpegImage>, the method itself wouldn’t know that it could only add JpegImages and not Bitmaps, defeating type saftey. Therefore the compiler does not let you do it. IEnumerable‘s type parameter is covarient, as seen by the out keyword, while ICollection‘s type parameter is not (since it is both in return positions and method parameter positions in the ICollection interface)

    public interface IEnumerable<out T>

    http://msdn.microsoft.com/en-us/library/dd799517.aspx

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

Sidebar

Related Questions

Imagine I have a method: void Method(bool parameter){ if(parameter){ // first case } else
I'm find I have several places that having public static inner classes designed that
Imagine you have an entity that has some relations with other entities and you
Imagine you have a large dataset that may or may not be filtered by
Imagine I have this code: var myFunc1 = function(event) { alert(1); } var myFunc2
Imagine i have this HTML snippet 4 times over for four different sections of
Is it possible to create an extension method that returns the instance that is
Does anyone have a good and efficient extension method for finding if a sequence
I have written an extension method in csharp for an MVCContrib Html helper and
Imagine you have one use case register contract and one extension attach document. The

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.