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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:32:12+00:00 2026-05-16T00:32:12+00:00

I have a helper class that does a simple but repetitive process on a

  • 0

I have a helper class that does a simple but repetitive process on a List of entities. For simplicity, it’s like this…

public static List<MyType> DoSomethingSimple(List<MyType> myTypes) {
    return myTypes.Where(myType => myType.SomeProperty.Equals(2)).ToList();
}

I now need to add support for another type, but everything is identical… how do I avoid an increasing list of overloaded methods like this:

public static List<MyType> DoSomethingSimple(List<MyType> myTypes) {
    return myTypes.Where(myType => myType.SomeProperty.Equals(2)).ToList();
}

public static List<MyOtherType> DoSomethingSimple(List<MyOtherType> myOtherTypes) {
    return myOtherTypes.Where(myOtherType => myOtherType.SomeProperty.Equals(2)).ToList();
}

… and so on.

  • 1 1 Answer
  • 1 View
  • 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-16T00:32:12+00:00Added an answer on May 16, 2026 at 12:32 am

    Here’s two ways:

    1. Use generics, and a common base class
    2. Use interfaces

    Method 1:

    public class BaseClass
    {
        public int SomeProperty { get; set; }
    }
    
    public class MyType : BaseClass { }
    public class MyOtherType : BaseClass { }
    
    public class ClassWithMethod
    {
        public static List<T> DoSomethingSimple<T>(List<T> myTypes)
            where T : BaseClass
        {
            return myTypes.Where(myType => myType.SomeProperty.Equals(2)).ToList();
        }
    }
    

    Method 2:

    public interface ICommon
    {
        int SomeProperty { get; set; }
    }
    
    public class MyType : ICommon
    {
        public int SomeProperty { get; set; }
    }
    
    public class MyOtherType : ICommon
    {
        public int SomeProperty { get; set; }
    }
    
    public class ClassWithMethod
    {
        public static List<T> DoSomethingSimple<T>(List<T> myTypes)
            where T : ICommon
        {
            return myTypes.Where(myType => myType.SomeProperty.Equals(2)).ToList();
        }
    }
    

    Now, if you try to make the method use the interface directly, like this:

    public class ClassWithMethod
    {
        public static List<ICommon> DoSomethingSimple(List<ICommon> myTypes)
        {
            return myTypes.Where(myType => myType.SomeProperty.Equals(2)).ToList();
        }
    }
    

    Then that would work if you have a List<ICommon> when you call it, but won’t work if you have a List<MyType>. In C# 4.0 this can be done if we change the method slightly:

    public class ClassWithMethod
    {
        public static List<ICommon> DoSomethingSimple(IEnumerable<ICommon> myTypes)
        {
            return myTypes.Where(myType => myType.SomeProperty.Equals(2)).ToList();
        }
    }
    

    Note that I changed to using an IEnumerable<ICommon> instead. The concept here is called Co- and contra-variance, and beyond that I’m not going to say much about it. Search Stack Overflow for more information on the subject.

    Tip: I would change the input parameter to be IEnumerable<T> regardless, since this would make your method usable in more instances, you could have different types of collections, arrays, etc. and as long as they contain the right type, they can be passed to the method. By limiting yourself to List<T> you force the user of your code to convert to a list in some cases. My guidelines are to be as unspecific as possible in input parameters, and as specific as possible in output parameters.

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

Sidebar

Related Questions

I have a helper class that creates some objects, like a builder. The helper
I'm creating a DirectX 11 helper class that looks kind of like this: #import
I have a helper class that I want to display a UIAlertView then will
I have a helper class that should only ever run in a background thread.
I have a helper class that is passed an array of values that is
I currently have a helper class that I am using to obfuscate a static
I have a REST service and I want to have a helper class that
Hi all im using a sqlite helper class, but i have a little problem
I have a nested form in a rails view that is called like this
I have written an ASP.NET HttpModule and I have a static helper class that

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.