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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T14:07:17+00:00 2026-05-10T14:07:17+00:00

C# doesn’t require you to specify a generic type parameter if the compiler can

  • 0

C# doesn’t require you to specify a generic type parameter if the compiler can infer it, for instance:

List<int> myInts = new List<int> {0,1,1,     2,3,5,8,13,21,34,55,89,144,233,377,     610,987,1597,2584,4181,6765};  //this statement is clunky List<string> myStrings = myInts.     Select<int,string>( i => i.ToString() ).     ToList<string>();  //the type is inferred from the lambda expression //the compiler knows that it's taking an int and  //returning a string List<string> myStrings = myInts.     Select( i => i.ToString() ).     ToList(); 

This is needed for anonymous types where you don’t know what the type parameter would be (in intellisense it shows up as 'a) because it’s added by the compiler.

Class-level type parameters don’t let you do this:

//sample generic class public class GenericDemo<T>  {     public GenericDemo ( T value )      {         GenericTypedProperty = value;     }      public T GenericTypedProperty {get; set;} }  //why can't I do: int anIntValue = 4181; var item = new GenericDemo( anIntValue ); //type inference fails  //however I can create a wrapper like this: public static GenericDemo<T> Create<T> ( T value ) {     return new GenericDemo<T> ( value ); }  //then this works - type inference on the method compiles var item = Create( anIntValue ); 

Why doesn’t C# support this class level generic type inference?

  • 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. 2026-05-10T14:07:18+00:00Added an answer on May 10, 2026 at 2:07 pm

    Actually, your question isn’t bad. I’ve been toying with a generic programming language for last few years and although I’ve never come around to actually develop it (and probably never will), I’ve thought a lot about generic type inference and one of my top priorities has always been to allow the construction of classes without having to specify the generic type.

    C# simply lacks the set of rules to make this possible. I think the developers never saw the neccesity to include this. Actually, the following code would be very near to your proposition and solve the problem. All C# needs is an added syntax support.

    class Foo<T> {     public Foo(T x) { … } }  // Notice: non-generic class overload. Possible in C#! class Foo {     public static Foo<T> ctor<T>(T x) { return new Foo<T>(x); } }  var x = Foo.ctor(42); 

    Since this code actually works, we’ve shown that the problem is not one of semantics but simply one of lacking support. I guess I have to take back my previous posting. 😉

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

Sidebar

Ask A Question

Stats

  • Questions 211k
  • Answers 211k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer What is the ItemsSource of your ListBox ? A list… May 12, 2026 at 10:13 pm
  • Editorial Team
    Editorial Team added an answer NO. CPU For software development, you need lots of cores.… May 12, 2026 at 10:13 pm
  • Editorial Team
    Editorial Team added an answer Try adding the following immediately before your cout statement: cin.clear();… May 12, 2026 at 10:13 pm

Related Questions

C# doesn't require you to specify a generic type parameter if the compiler can
C# doesn't allow structs to derive from classes, but all ValueTypes derive from Object.
Objective-C doesn't have namespaces, and many (such as CocoaDevCentral's Cocoa Style Guide ) recommend
Since C# doesn't have a before,after,last,first etc. as part of its foreach. The challenge
string.insert in c# doesn't overwrite the character that is in the startindex does it?

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.