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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:35:30+00:00 2026-06-14T05:35:30+00:00

Wondering why this doesn’t work. Insight appreciated. static void Main(string[] args) { List<int> foo

  • 0

Wondering why this doesn’t work. Insight appreciated.

    static void Main(string[] args)
    {
        List<int> foo = new List<int> { 1, 2, 3 };
        var myResult = MyTest<int>(foo);
    }

    private static List<int> MyTest<T>(List<T> input)
    {
        List<int> bar = new List<int> { 2, 3, 4 };
        return bar.Where(b => input.Contains(b)).ToList();
    }

Expected output from MyTest() is a List { 2, 3 }. However, the compiler reports two errors on input.Contains(b), as follows:

  1. Argument 1: cannot convert from ‘int’ to ‘T’

  2. The best overloaded method match for ‘System.Collections.Generic.List.Contains(T)’ has some invalid arguments

This Where() clause works fine if I don’t use generic lists.

This is a simplification of my real-world problem, so please don’t get stuck on “why are you writing this?” The problem is the error and why it’s occurring.

Revised for (hopefully) clarity:

namespace SandBox
{

class Foo
{
    public int FooInt { get; set; }
    public string FooString { get; set; }
}

class Program
{
    private static List<Foo> fooList = new List<Foo> {
            new Foo() {FooInt = 1, FooString = "A"},
            new Foo() {FooInt = 2, FooString = "B"},
            new Foo() {FooInt = 3, FooString = "C"}
    };

    static void Main(string[] args)
    {
        List<int> myIntList = new List<int> { 1, 2 };
        var myFirstResult = GetFoos<int>(myIntList);

        List<string> myStringList = new List<string> { "A", "B" };
        var mySecondResult = GetFoos<string>(myStringList);
    }

    /// <summary>
    /// Return a list of Foo objects that match the input parameter list
    /// </summary>
    private static List<Foo> GetFoos<T>(List<T> input)
    {
        //***
        // Imagine lots of code here that I don't want to duplicate in 
        // an overload of GetFoos()
        //***

        if (input is List<int>)
        {
            //Use this statement if a list of integer values was passed in
            return fooList.Where(f => input.Contains(f.FooInt));
        }
        else if (input is List<string>)
        {
            //Use this statement if a list of string values was passed in
            return fooList.Where(f => input.Contains(f.FooString));
        }
        else
            return null;
    }
}
}

The same compiler errors are reported on input.Contains(f.Property).

  • 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-06-14T05:35:31+00:00Added an answer on June 14, 2026 at 5:35 am

    Try this:

    static void Main(string[] args)
    {
        List<int> foo = new List<int> { 1, 2, 3 };
        var myResult = MyTest<int>(foo);
    }
    
    private static List<int> MyTest<T>(List<T> input)
    {
        List<int> bar = new List<int> { 2, 3, 4 };
        return bar.Where(b => input.OfType<int>().Contains(b)).ToList();
    }
    

    The problem is that at the compiler has no idea what type T is. Since T could be anything you can’t call a method which expects an int (input.Contains).

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

Sidebar

Related Questions

I'm wondering why this code doesn't work: void KeyValueList::Release() { //(m_ppKeyValueList is a dynamic
I'm experimenting with knockout and the mapping plugin and wondering why this doesn't work.
Just wondering why something like this doesn't work: public function address($name){ if(!isset($this->addresses[$name])){ $address =
This doesn't work: Debug.Assert(Thread.CurrentThread.Name == Main Thread); //doesn't work //name is null despite name
Im just learning javascript and I'm just wondering why this doesn't work. I've created
I've noticed that this doesn't work for me and I'm wondering if it's just
So I was wondering why this doesn't work. I have created a settings bundle,
I was wondering why this doesn't work: Is it possible to declaratively bind to
Just wondering is this kind of code recommended to increase performance? void functionCalledLotsofTimes() {
Just wondering why this works: window.NewListView = Backbone.View.extend({ template: _.template('<a href=/list class=button new-list>Create New

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.