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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:08:56+00:00 2026-05-25T11:08:56+00:00

In Java I can have a method declaration something like this: <T> List<T> getList(final

  • 0

In Java I can have a method declaration something like this:

<T> List<T> getList(final Class<T> objectClass, String whatever)

Which means that I specify the list return type by specifying the Class to the method.

List<Customer> customers = getList(Customer.class, "blah");

And if I don’t specify the classes properly I get a compile time error. (which is what I’m looking for – I want the compiler to catch a mismatch).

List<String> customers = getList(Customer.class, "blah"); // will not compile

What’s the equivalent of this in C#?
TIA

  • 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-25T11:08:56+00:00Added an answer on May 25, 2026 at 11:08 am

    There is no way in C# to have the compiler infer the generic type based on the return type.

    In C#, you must specify T if the only differentiation is the return type:

    List<Customer> customer = getList<Customer>("blah");
    

    This method would be written as:

    List<T> getList<T>(string whatever) { ... }
    

    However, in C#, type inference is automatically handled if there is a parameter that takes a type of customer. For example, you can have:

    List<T> MakeList<T>(params T[] items) { ...}
    

    Then call this as (without <Customer>):

     Customer one = GetCustomer(1);
     Customer two = GetCustomer(2);
    
     var customers = MakeList(one, two);
    

    Edit in response to comment:

    If you are going to be constructing a new “Customer” inside of your method, and want this to work on any type, you’ll need a new constraint in place. To have this:

    List<Customer> customers = GetList<Customer>("blah");
    

    You would need something like:

    List<T> GetList<T>(string whatever) 
       where T : new() // This lets you construct it internally
    {
         List<T> results = new List<T>();
    
         /// ...
         T newInstance = new T();
    
         results.Add(newInstance);
         return results;
    }
    

    That being said, if you’re going to make a method like this, chances are you’ll also want to have a constraint to an interface, so you can setup the object you create:

    List<T> GetList<T>(string whatever) 
       where T : ISomeInterface, new()
    

    This will let you use properties of ISomeInterface within the method, as well as limit it to only working with types that implement that interface.

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

Sidebar

Related Questions

like in java I have: Class.getSuperClass().getDeclaredFields() how I can know and set private field
I have a method with this signature: protected final Map<String, Object> buildOutputMappings( AbstractDataObject ado,
This is on Java 6 Can I have a common method to handle my
I'm used to the Java model where you can have one public class per
By convention, a static method specifically in Java can have access only to static
Why can't Java classes have abstract fields like they can with abstract methods? For
Why can't we have static method in a non-static inner class? public class Foo
On a Java portal you can have portlets that include data provided by other
I have noticed in Java that you can have a function with object... as
I am learning Java and just found that the Interface can have fields, which

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.