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

  • Home
  • SEARCH
  • 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 8785515
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:23:02+00:00 2026-06-13T21:23:02+00:00

Ok I must have got the title terribly wrong. More code, less words: public

  • 0

Ok I must have got the title terribly wrong. More code, less words:

public class Manager<T> where T : Control, new()
{
    void Manage()
    {
        Do(t => IsVisible(t));
    }

    bool IsVisible(T t)
    {
        return t.Visible;
    }

    S Do<S>(Func<T, S> operation)
    {
        return operation(new T());
    }
}

The compiler is happy about Do. It can infer the T type easily. Now lets say I have this:

public static class Util
{
    static void Manage()
    {
        Do(t => IsVisible(t)); //wiggly line here
    }

    static bool IsVisible<T>(T t) where T : Control
    {
        return t.Visible;
    }

    static S Do<S, T>(Func<T, S> operation) where T : Control, new()
    {
        return operation(new T());
    }
}

The compiler wants types to be explicitly typed now. Here is what I think:

In the first class T was easily inferable from IsVisible method which had T overload and T is known all over the Manager class, no big deal. But in the second case T is specified as a generic constraint on the method and may be that’s harder to infer. Ok.

But this doesn’t work either:

public static class Util
{
    static void Manage()
    {
        Do(t => IsVisible(t)); //still wiggly line
    }

    static bool IsVisible(Control t)
    {
        return t.Visible;
    }

    static S Do<S, T>(Func<T, S> operation) where T : Control, new()
    {
        return operation(new T());
    }
}
  1. Why doesn’t compiler infer T in the last case?

  2. More importantly, how different is the last case from the first? In the first case compiler have to infer it from IsVisible method and then go all the way back to check what T is in the class containing IsVisible, where as in the last case its readily available in IsVisible method. So I assume third case is easier than the first.

  • 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-13T21:23:04+00:00Added an answer on June 13, 2026 at 9:23 pm

    (First case)

    The compiler is happy about Do. It can infer the T type easily.

    It’s not inferring T at all. T is a type parameter for the class:

    public class Manager<T> where T : Control, new()
    

    (Second case)

    The compiler wants types to be explicitly typed now.

    I assume you mean in the Manage code:

    Do(t => IsVisible(t))
    

    And that’s right. What do you think the type of T should be here? How would you expect the complier to infer it?

    (Third case, where the method is IsVisible(Control t))

    Why doesn’t compiler infer T in the last case?

    It can’t do so from just the parameters. It sounds like you’re expecting it to work out every type for which the body of the lambda expression could work… and type inference simply doesn’t work that way. You can easily give the compiler enough information though:

     Do((Control t) => IsVisible(t)); 
    

    More importantly, how different is the last case from the first?

    In the first case, T isn’t a type parameter for the Do method. The compiler only needs to infer S, which it can do from the return type of the lambda expression. It doesn’t need to perform any inference for T, because that’s already “known”. (It’s still generic, but it’s not something which needs to be inferred for that method call.) The type of T will need to be supplied when constructing an instance of Manager in the first place, so it’s effectively moving that decision.

    For all the gory details of type inference, see section 7.5.2 of the C# 4 spec (or the equivalent section in the C# 3 or C# 5 specs). I’d advise a strong cup of coffee first though 🙂

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

Sidebar

Related Questions

I must have pressed on the wrong button at some point, and now when
I must have done something wrong. I have a C# project in Visual Studio
you must have seen this piece of code somewhere else too,but obviously there's no
I have got a method in my MVC 3 controller class, that accepts two
I have got a Class Library project where I keep some interfaces and classes
I have a problem. You see, I've got this code (MVC3) @Using Html.BeginForm() @Html.ValidationSummary(True,
I have a C# annotation which is : [AttributeUsage(AttributeTargets.Method)] public class OperationInfo : System.Attribute
I find myself in a situation that someone else must have got stuck in
I have got the following code: - (void)tableView:(UITableView *)tableView commitEditingStyle: (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
I have the models: class Article(models.Model): title = models.TextField(blank=True) keywords = models.ManyToManyField(Keyword, null=True, blank=True)

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.