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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:42:37+00:00 2026-06-18T09:42:37+00:00

Maybe it is pretty simple, but I’m not in use with the type Type

  • 0

Maybe it is pretty simple, but I’m not in use with the type Type and it’s uses.

Say I want to create a List<T> either with T=Double or T=UInt32, depending on the result of some function, say public static Type CheckType(String input);

In code:

Type t = Program.CheckType(someInput); // it returns typeof(Double) or typeof(UInt32)
if (t == typeof(Double))
    List<Double> l = new List<Double>();
else
    List<UInt32> l = new List<UInt32>();

I know the above code won’t compile because I’m letting l to have two different meanings (a list of double and a list of unsigned int)… So it leads to my question:

  • Is there a way to avoid the if in the above code?

Something similar to this:

Type t = Program.CheckType(someInput);
List<t> l = new List<t>(); // I know this won't compile either...

I mean, that would generically instantiate a generic List…

Thank you in advance!

EDIT:

This is NOT a duplicate of the marked question for only one reason: Double and UInt32 are not Anonymous types! The question, here, is how to determine the type of some input data (which will be Type T=typeof(Double) or Type T=typeof(UInt32), for example) and, thus, create a generic SampleClass<T> based on that input data type, T!

In other words: determine some Type T in the runtime and, then, instantiate a generic type with the determined type T. Sorry if I didn’t make that clear before…

PS: I used List<T> as an example for SampleClass<T>.

  • 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-18T09:42:38+00:00Added an answer on June 18, 2026 at 9:42 am

    You can’t type the list as generic, since you don’t know the type parameter, but you can create a List instance at runtime.

    Type t = Program.CheckType(someInput);
    Type listType = typeof(List<>).MakeGenericType(t);
    IList list = (IList) Activator.CreateInstance(listType);
    

    If you try to add an object of the incorrect type, you will get an exception, so this approach has an advantage over using a collection like ArrayList, or List<object>

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

Sidebar

Related Questions

This ought to be pretty simple, maybe use a regex but I would think
Pretty simple questions. And yes, maybe not (that) important, but I'm really curious what
Maybe my title is not the best but here is what I really want.I
This seems pretty simple and maybe I'm just overlooking the proper flag , but
Pretty simple function here but not working. Just started with Javascript so be gentle
I am new to Android development so maybe I am missing something pretty simple,
I thought I was pretty knowledgeable about CSS but this simple problem baffles me.
I use this simple script : <body> <script type=text/javascript> function mouseOver() { document.getElementById(img1).src =images/p2.jpg;
Pretty simple question that I couldn't find an answer to, maybe because it's a
This may be pretty simple but it's stumping me. I'm trying to return a

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.