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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:16:44+00:00 2026-06-15T01:16:44+00:00

I’m getting a strange error with Visual Studio 10 (and now 11 as well).

  • 0

I’m getting a strange error with Visual Studio 10 (and now 11 as well). I have an extension method

public static S Foo<S, T>(this S s) where S : IEnumerable<T>
{
    return s;
}

Now if I call

"".Foo(); // => 'string' does not contain a definition for 'Foo' and no extension method 'Foo' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)

I’m not at all understanding what’s happening under hood. The annoying part is that the intellisense lists Foo for IEnumberable<T>s. At best it should have given a type can't be inferred error.

If I call it this way:

Extension.Foo(""); // => The type arguments for method 'Extension.Foo<S,T>(S)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

Why cannot type be inferred in the above case?

More:

Suppose I have:

public static S Foo<S, T>(this S s, T t) where S : IEnumerable<T>
{
    return s;
}

And if I call:

"".Foo(1);

The type inference is so smart here to tell me that Foo should be returning IEnumerable<int> and string is not all that!!

So if compiler can know Foo is expecting a char as the first argument, then why doesn’t my first example just compile? In other words why is that in the first example the compiler know T in that case is char?

As expectedly this works for the second example:

"".Foo('l');

I’m just wondering why can’t T be inferred as char in first example, after all string is IEnumberable<char>.


Edit:

I got the answer from SLaks. But it’s so strange that C# doesn’t do this (kind of type inference) considering compiler takes into account the generic constraints as well when exposing the available methods to operate on an object.

In other words:

public static S Foo<S, T>(this S s)
{
    return s;
}

makes Foo available on all objects.

public static S Foo<S, T>(this S s) where S : IEnumerable<T>
{
    return s;
}

makes Foo available on all IEnumerable<T>s since it knows S is IEnumerable<T>. So I was thinking C# will be even inferring the type of T! Thanks everyone! 😉

  • 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-15T01:16:45+00:00Added an answer on June 15, 2026 at 1:16 am

    The type inference engine isn’t smart enough to do that.

    C# type inference only looks at the method signature.
    Generic constraints are not part of the signature.

    Since T is not used directly in the signature, the compiler will not infer it.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
This could be a duplicate question, but I have no idea what search terms
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and

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.