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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:21:01+00:00 2026-05-24T03:21:01+00:00

I’ve noticed that the C# compiler doesn’t infer second generic parameter. Example: C++ template

  • 0

I’ve noticed that the C# compiler doesn’t infer second generic parameter.
Example:

C++ template code: (yea I know that templates don’t work like generics)

class Test {
public:
template <class T,class V> 
    T test(V v) {
       //do something with v
       return T();
    }
};

int i = 0;
Test t = new Test();
double j = t.test<double>(i); //infers V as int

The templates (and generics) can’t infer return type, so in C++ I give it the first template parameter, and the second template parameter is inferred from the variable type.

Now, same example in C#:

class Test {
    public T test<T,V>(V v) where T: new() {
       //do something with v
       return new T();
    }
};

int i = 0;
Test t = new Test();
double j = t.test<double>(i); //Error Using the generic method 'Test.test<T,V>(V)' requires '2' type arguments

But if i use 1 type, I don’t have to explicitly specify the type:

class Test {
    public V test<V>(V v) where V: new() {
       return new V();
    }
};

int i = 0;
Test t = new Test();
int j = t.test(i); //OK infers V as int.

So, why can’t C# generics infer the second type (while in c++ templates it clearly can) ?
I’m sure it’s designed that way (I doubt they the .Net team overlooked this), so why is it designed this way that I must explicitly specify both types?

Edit:

From the discussions we had in the answers so far, both languages support overloading by number of template parameters.

So again, why is C# designed this way ? What’s different in the language implementation that doesn’t allow to explicitly declare only one parameter ?

  • 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-24T03:21:02+00:00Added an answer on May 24, 2026 at 3:21 am

    C# has been designed to be a slightly less brain-bending language than C++.

    In particular, I don’t think it’s a great idea to compare C# generics to C++ templates for various reasons – they’re fundamentally two really quite different approaches to accomplishing similar things in some situations. The C++ approach is certainly flexible in some ways – although it doesn’t allow (as I understand it) templates which only exist in binary form, or new template specializations to be created at execution time. Basically the C++ templating approach doesn’t sit well with the rest of how .NET fits together.

    Now as for why you can’t specify some type arguments and allow others to be inferred (which is a language decision rather than a platform decision; I’m sure it would be feasible as far as .NET itself is concerned) – again, I believe this is for the sake of simplicity. Choosing the exact right method and the right type arguments is already extremely complicated in C# – more complicated than most C# developers can get their heads round. It involves:

    • Potentially considering methods up the type hierarchy from the compile-time type of the target
    • Overloading by number of parameters
    • Overloading by the number of type parameters
    • The effect of named arguments
    • The effect of optional parameters
    • The effect of generic type parameter constraints on parameter types (not constraints specified by the target method, note)
    • Method group to delegate conversions
    • Anonymous function conversions
    • Type inference for type arguments
    • Dynamic typing
    • Generic covariance and contravariance

    Personally, I think that’s enough to get my head around, without allowing yet more possiblities via “M can still be a candidate if it has at least as many type parameters as specified type arguments”. Would you also want named type arguments and optional type parameters? 😉

    I’ve looked at overloading quite a lot, following the spec thoroughly etc. I’ve found areas which make the language designers scratch their heads and try to work out what the compiler should do. I’ve found areas which the compiler definitely gets wrong. I wouldn’t want to add any more complexity here without a really good reason.

    So yes, it’s basically for the sake of simplicity, and sometimes that’s a pain – but often you can work around it. For every potential feature, you need to consider:

    • The benefit of the feature to end developers
    • The cost of the feature to end developers in terms of time spent understanding it
    • The cost to the language designers in designing and specifying it thoroughly
    • The cost to the compiler writers in implementing it correctly
    • The cost to the test team in testing it thoroughly (in conjunction with everything else around overloading)
    • The cost to future potential features (if this one makes the language more complicated, that leaves less “potentially grokable” additional complexity for other features)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
Does anyone know how can I replace this 2 symbol below from the string
i got an object with contents of html markup in it, for example: string
I am trying to understand how to use SyndicationItem to display feed which is

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.