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

The Archive Base Latest Questions

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

Sorry – not sure of a better name, please modify if you can think

  • 0

Sorry – not sure of a better name, please modify if you can think of better.

I am trying to learn a bit more about IEnumerable/collections/generics and I thought I was getting somewhere, until this example got me:

 var nums = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 10 };

 var result = FilterNums(nums, i => i % 2 != 0);

…..

public static IEnumerable<T1> FilterNums<T1>(IEnumerable<T1> numslist, Func<T1, bool> predicateDelegate)

…..

Why does the call the call to FilterNums work? If I change it to FilterNums<int>, it still works and that is what I actually expected to type.

So, is it some how detecting the T1 for the lambda query and not requiring me to write it or is something else going on?

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

    The type inference process is a bit complicated; see chapter 7 of the C# spec for exact details.

    Briefly it works like this.

    When you invoke a method without a generic parameter list, first we make a set of all the accessible methods of that name.

    Next we check to see if any of them are generic. If they are then we try to see if the generic type arguments can be inferred from the actual arguments. The inference process goes like this. Suppose you have an argument list A:

    A: (nums, i => i%2 != 0)
    

    and a list of formal parameter types P:

    P: (IEnumerable<T1>, Func<T1, bool>)
    

    and a set of generic type parameters X:

    X: <T1>
    

    The goal of type inference is to make inferences from every member of A to the corresponding member of P in order to deduce enough information about each member of X.

    This particular problem is easy. From the first argument we see that the type of nums is int[]. We see that the first formal parameter in P is IEnumerable<T1>. We know that int[] is convertible to IEnumerable<int>, and therefore T1 might be int. We make a note of that fact.

    And at this point, we’re basically done. We have nothing about T1 that we can infer from the second argument/parameter pair. Type inference succeeds, and determines that
    T1 is int. So we pretend that you called it with <int> as the type argument list.

    That was a very simple type inference problem. Consider this one:

    A: (customers, c=>c.Name)
    P: (IEnumerable<T>, Func<T, R>)
    X: <T, R>
    

    This is the problem you get when you do customers.Select(c=>c.Name).

    What do we do? From the first argument we deduce “customers implements IEnumerable<Customer>, therefore T is probably Customer”. After making that deduction we can then say “c in the lambda is therefore Customer. Therefore this is a lambda from Customer to whatever the type of Customer.Name is. That’s string. Therefore R is string”.

    See how in that case one inference had to be chained onto the next; we cannot simply do inference “in parallel” because one inference might depend on the results of another. Those dependencies can contain loops and other odd topologies. The exact details of how we make progress through that chain of dependencies is a bit complicated; see the spec for details.

    We also have to deal with the case where two or more bounds are deduced for a type parameter, and whether those bounds are “upper”, “lower” or “exact”.

    If this subject interests you, I’ve written about it extensively. See

    http://blogs.msdn.com/b/ericlippert/archive/tags/type+inference/

    for lots of articles on various aspects of type inference, not just generic method type inference. For a video of me explaining how method type inference works in C# 3.0, see:

    http://wm.microsoft.com/ms/msdn/visualcsharp/eric_lippert_2006_11/EricLippert01.wmv

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

Sidebar

Related Questions

(Sorry for the not-so-good-title, I'm not sure how my problem can be solved, thus
Sorry about the title to this question, it's not very clear but wasn't sure
Sorry but I am not sure how to ask this question but I am
Sorry to bother you all here but I am still trying to understand more
sorry if the title is worded oddly, not really sure how to say it.
(Sorry about the title, couldn't think of how to explain it) So I have
Sorry if the title is poorly descriptive, but I can't do better right now
Sorry for the odd wording, I'm not sure how to describe what I need
Sorry for question but I can't find answer anywhere on internet. I couldn't find
Sorry for this simple question, but I can't solve it... There is an example:

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.