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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T23:41:55+00:00 2026-05-20T23:41:55+00:00

I want to write an extension method for a collection of objects that uses

  • 0

I want to write an extension method for a collection of objects that uses base class as a type requirement. I understand this is not necessarily the best way to do things, but I am curious because I’m interested in learning the nuances of the language. This example explains what I would like to do.

public class Human { public bool IsHappy { get; set; } }
public class Man : Human { public bool IsSurly { get; set; } }
public class Woman : Human { public bool IsAgreeable { get; set; } }

public static class ExtMethods
{
    public static void HappinessStatus(this IEnumerable<Human> items)
    {
        foreach (Human item in items)
        {
            Console.WriteLine(item.IsHappy.ToString());
        }
    }
}

// then in some method, I wish to be able to do the following

List<Woman> females = RetreiveListElements(); // returns a list of Women
females.HappinessStatus(); // prints the happiness bool from each item in a collection

The only way I can get the extension method to expose is to create a collection of Humans. Is it possible to call this type of extension method on derived types as long as I only reference members of the base type?

  • 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-20T23:41:56+00:00Added an answer on May 20, 2026 at 11:41 pm

    Your code will actually compile as is with the C# 4 compiler, as that version supports contravariant type parameters.

    To get it working with C# 3, you can create a generic extension method for IEnumerable<T> with a where T : Human constraint that acts on the generic type, instead of specifically only for IEnumerable<Human>:

    public static void HappinessStatus<T>(this IEnumerable<T> items) where T : Human
    {
        foreach (T item in items)
        {
            Console.WriteLine(item.IsHappy.ToString());
        }
    }
    

    Then you can call the extension method on your List<Woman> collection as you describe.

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

Sidebar

Related Questions

HashSet does not have an AddRange method, so I want to write an extension
I want to write a Songbird extension binds the multimedia keys available on all
I want to write a Swing application in Griffon but I am not sure
I want to write a command that specifies the word under the cursor in
I want to write a function in Python that returns different fixed values based
I want to write a function that takes an array of letters as an
I want to write a word addin that does some computations and updates some
I want to write some JavaScript that will change the onmousedown of a div
I am looking for a built-in extension method like Apply: collection.Apply (predicate) which will
I want to write a webservice that will upload a file to our server

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.